Catch the window.open from javascript

前端 未结 3 617
自闭症患者
自闭症患者 2021-01-21 15:05

I have a web page into a html iframe, the web page has a javascript function to open links, that function use the window.open method to open a new window.

I cannot modif

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-21 15:39

    Here's a similar snippit I've been working on... it was tricky getting the contentWindow right, but maybe this provides some insight?

    //get a reference to the iframe DOM node
    var node_iframe = document.getElementById("myiframe");
    
    //get a reference to the iframe's window object
    var win_iframe = node_iframe.contentWindow;
    
    //override the iframe window's open method
    win_iframe.open = function(strUrl,strName,strParams){
    
        /* create an object (to substitute for the window object) */
        var objWin = new Object;
    
        /* save the open arguments in case we need them somewhere */
        objWin.strUrl = strUrl;   
        objWin.strName = strName; 
        objWin.strParams = strParams; 
    
        /* create a blank HTML document object, so any HTML fragments that 
         * would otherwise be written to the popup, can be written to it instead */
        objWin.document = document.implementation.createHTMLDocument(); 
    
        /* save the object (and document) back in the parent window, so we 
         * can do stuff with it (this has an after-change event listener that 
         * pops a YUI Panel to act as a kind of popup) -- NOTE: the object in 
         * the parent window that we're saving this to has YUI Attribute installed
         * and listens to changes to the objPopupWindow attribute... when it 
         * gets changed by this .set() operation, it shows a YUI Panel. */
        parent.MyCustomClassObjectWithYUIAttributes.set('objPopupWindow', objWin); 
    
        /* return the object and all its members to whatever was trying to 
         * create a popup window */
        return objWin; 
        };//end override method definition
    

提交回复
热议问题