Handle keyPress Accross Frames in IE

前端 未结 2 1109
醉梦人生
醉梦人生 2021-01-25 07:24

I\'ve been trying to handle the onkeydown event across multiple frames (no, I unfortunately cannot be rid of the frames) via javascript (see my previous questio

2条回答
  •  旧时难觅i
    2021-01-25 07:50

    I got this to work

    frameset.html

    
    
    
        Test
    
    
        
        
    
    
    

    frame1.html

    
    
    
        Frame 1
        
    
    
        frame1
          
    
    
    

    frame2.html

    
    
    
        Frame 2
    
    
        frame2
        
    
    
    

    First of all, it always pays to be fairly explicit with your object references. Using proprietary DOM shortcuts (such as window.frameName) just add unnecessary error potential. This is why my script explicitly looks in the frames collection of the window object.

    Next is to familiarize yourself with the various built-in window references in the DOM when dealing with framesets. There are 4 total

    1. window - The current window. This is also implied when left out (i.e., window.onload === onload)
    2. parent - The parent window to the current
    3. top - The topmost window in a frameset family. parent === top when you have only one frameset.
    4. self - alias of window

    So basically what I've done here is, when the onload event fires in the frame1 window, is add a handler function to the keydown event for the documents in both of the frame windows.

    That function uses a closure to ensure that the event generated in frame1 is available to the actual handler, regardless of which window generated the keydown event. This is necessary because of how IE does events. Whereas other browser create new event objects as they occur and passes them to event hanlders, IE just modifies the global event object (referenced via window.event or more simple, just event) to reflect the current event.

    I hope that makes sense.

提交回复
热议问题