Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    pass event to parent

    How can I pass an event from a frame to its parent? I want to be able to assign the event from the parent (document.frames(0).document.onclick = go) but I cant even get the function to work right when it is assigned in the frame itself. The function in the parent is getting called but the event isnt getting through. It works on the inline call on the image in the frame but I cant get it to work via script.

    Frame:
    Code:
    <html>
    <head>
    <script language="JavaScript1.2">
    //document.onclick = parent.go;
    document.onclick = parent.go(event);
    //-->
    </script>
    </head>
    <body>
    <img name="image1" src="images/1.jpg" width="100" height="100" onclick="parent.go(event)">
    </body>
    </html>
    Parent:
    Code:
    <html>
    <head>
    <script language="JavaScript1.2">
    document.onclick = go;
    //document.frames(0).document.onclick = go;
    
    function go(event){
    alert(event.type);
    return false;
    }
    //-->
    </script>
    </head>
    <body>
    <iframe id="frame1" name="frame1" src="frame1.htm" width="240" height="175" border="1" frameborder="1"></iframe>
    </body>
    </html>

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    i've not tested this but theoretically it should work:
    Code:
    document.onclick = function(e) {
      parent.go(e || window.event);
    }
    hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes thanks,
    Code:
    document.onclick = function(e) {  parent.go(e || window.event);  }
    does work in the frame. Now is there a way to set this from the parent?

    I would have thought this would work:

    Code:
    <html>
    <head>
    <script language="JavaScript1.2">
    function setframe(){
    document.frames(0).document.onclick = function(e) {  parent.go(e || window.event);  }
    }
    
    function go(event){
    alert(event.type);
    return false;
    }
    //-->
    </script>
    </head>
    <body>
    <iframe id="frame1" name="frame1" src="frame1.htm" width="240" height="175" border="1" frameborder="1" onload="setframe()"></iframe>
    </body>
    </html>
    I had to call it from the onload event of the iframe, I assume its because the frame object has to exist before I can set it.

    When I run it I get an object required error in the go function. The function is getting called by the frame but the event isnt getting passed or something.

    Im definately confused because if I look at the DOM, the value of document.frames(0).document.onclick is identicable wether I do it from the frame or the parent but it works one way and not the other. Am I missing something?

  • #4
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Again untested but theoretically:

    in parent:
    Code:
    window.onload = function() {
      document.frames[0].contentWindow.document.onclick = function(e) {
        parent.go(e || window.event);
      }
    }
    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #5
    New Coder
    Join Date
    Jun 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Couldnt get that one to work, I kept getting a document.frames.0.contentWindow.document is null or not an object error. I think its becuase the frame doesnt exist at window.onload.

    I also tried replacing
    Code:
    document.onclick = function(e) {  parent.go(e || window.event);  }
    in the parent with
    Code:
    document.frames[0].contentWindow.document.onclick = function(e) {
        parent.go(e || window.event);
    but then I get an object doesnt support this property or method error.

    Im also wondering that even if I get it to work from the parent correctly, what happens if the frame gets refreshed? Would the document.onlick of the frame get cleared if it was only assigned by the javascript in the parent?

    Maybe Im stuck doing it in the frame.

    Thanks for the help though, Im getting closer.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •