从JavaScript中捕获window.open

4

我有一个包含在html iframe中的网页,该网页具有一个javascript函数用于打开链接,该函数使用window.open方法打开一个新窗口。

我无法修改javascript函数(该页面是由mapguide创建的),因此我想在iframe外部捕获该调用,将新窗口的内容放入ajax模态框架中,而不是打开新窗口,这可能吗?


如果这些框架来自同一域和端口,您可以覆盖window.open函数以执行您想要的其他操作。 - Gabriel Gartz
我忘了提到我正在使用asp.net、c#制作我的网页。 - Naty Bizz
3个回答

3
尽管我一般不推荐这样做,但是你可以在iframe中覆盖window.open函数的定义,前提是你的页面和iframe在同一个域中以避免XSS安全错误。
HTML:
<iframe id="myFrame" src="...">
</iframe>

父窗口中的JavaScript:

var frame = document.getElementById('myFrame');

if (frame) {
    frame.contentWindow.open = function (url, windowName, windowFeatures) {
        // do whatever you want here (e.g. open an ajax modal frame)
    };
}

我尝试过这个,但在执行“frame.contentWindow.open”行后,我收到了“frame.window为null或不是对象”的错误消息; 我在Internet Explorer 8上运行此代码:var d = document.getElementById(“ifr”); //我的框架 如果(d){ var ifropen = d.contentWindow.open; d.contentWindow.open = function(url,windowName,windowFeatures){ alert(windowFeatures); return orgOpen(url,windowName,windowFeatures); } d.window.open(“http://stackoverflow.com”);} - Naty Bizz
@NatyBizz 在你给 d.contentWindow.open 赋予新函数后,你漏掉了一个 ;,而在调用 d.window.open 时,你多加了一个 ; - jbabey

0
我会假设“mapguide”内容是从与包含iframe的页面不同的域提供的。
如果是这样,你需要“代理”“mapguide”内容 - 即:你的 iframe 将需要从另一个脚本 URL(我将在此示例中假设为 PHP)加载 mapguide 内容,该脚本代码将从任何地方获取“mapguide”软件。这一部分很容易,服务器端代码可能如下所示:
<? 
    $content = file_get_contents('http://mapguide.domain/path/to/contents');

    // alter content here

    print $content;
?>

iframe的src属性应该指向您服务器上包含该代码的PHP文件(而不是“mapguide”服务器)。

如果“mapguide”内容包含HTML链接、加载CSS/JavaScript文件或进行AJAX调用,则需要让您的服务器端代码重写这些URL,以便引用回您的服务器。这部分并不容易,而且真正取决于“mapguide”JavaScript的复杂程度。

因此,在上面的评论中说“在此更改内容”之后,您需要对$content中包含的HTML进行一些可怕的正则表达式替换(或解析和重新生成),目标是将每个URL都通过您的“代理”PHP脚本传递,并从“mapguide”服务器上的适当位置加载。

如果您成功完成了所有这些工作,那么您的iframe将从包含HTML页面相同的域中提供服务,因此外部页面的JavaScript将能够替换iframe的window.open函数--以防止弹出窗口,或者执行您想要的任何操作--就像@jbabey在另一个答案中所说的那样。

所有这些都是基于“mapguide”内容没有用户协议和/或版权政策的前提下,禁止“抓取”(自动复制)其(“mapguide”内容作者)内容。

0

这是我正在处理的类似片段……获取 contentWindow 很棘手,但也许这能提供一些见解?

//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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接