How to Pass events between iframes and functions?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ensi

    #1

    How to Pass events between iframes and functions?

    Hello,

    I have a main html page which creates an iframe, something like:
    Code:
    <iframe id='somefrm' src="http://somepage/frm.html" title="My frame" name='somefrm' frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 34%;">text</iframe>
    Now, the above iframe has for example this in it:
    Code:
    <body>
    <a href="http://msn.com">First link inside my frm.html</a><br />
    <a href="http://msn.ca">Second link inside my frm.html</a><br />
    </body>
    I want to add an event to the links from this iframe from my main page like this:
    Code:
    window.frames[0].document.getElementsByTagName('a')[0].onmousemove=Functi;
    window.frames[0] refers to the first frame (assuming that's the above). In the above line I am assigning the first anchor in the iframe the onmousemove attribute and tell it to execute the function Functi when a user moves the mouse over that link in the iframe.

    This works almost well. The function is called but the problem is that I need to operate on the event that's generated and hence need a reference to it.
    The function Functi looks something like: and it is declared in the main html page:
    Code:
    function Functi(e) {
    	if (!e) var e = window.event;
    	// operate on e
    }
    Now, the problem is how can I correctly pass the 'event' that happened in the iframe to the function Functi?

    I am sure that the problem lies somewhere with the iframes because assigning exactly in this same way the onmousemove attribute to a link in my main page works perfectly fine. So, this works well:
    Code:
    document.getElementsByTagName("a")[1].onmouseover=Functi;
    The above will give the onmouseover property to the second anchor in my main page, and when I hover over it, it calls the function and accesses the 'event' without problems.

    I tried doing something like:
    Code:
    window.frames[0].document.getElementsByTagName('a')[0].onmousemove=function(){Functi(event);};
    But it doesn't help.

    The error I get when I hover over the link in my iframe is: "Object expected" on the line that tries to access the event in the function Functi.


    Thanks in advance for your replies.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    you might try:

    Code:
    onmousemove = function(e) {
        var event = !e ? window.event : e;
    
        Functi(event);
    };
    this should always pass the event to the Functi() method.
    Last edited by gits; Oct 30 '10, 08:46 AM. Reason: fix quick shot :)

    Comment

    Working...