从javascript捕获window.open
我把一个网页变成了一个html iframe,这个网页有一个javascript函数来打开链接,这个函数使用window.open方法来打开一个新窗口从javascript捕获window.open,javascript,html,iframe,Javascript,Html,Iframe,我把一个网页变成了一个html iframe,这个网页有一个javascript函数来打开链接,这个函数使用window.open方法来打开一个新窗口 我无法修改javascript函数(页面是用mapguide创建的),因此我希望在iframe之外捕获该调用,将新窗口的内容放入ajax模式框架,而不是打开新窗口,这可能吗?我假设“mapguide”内容来自不同的域,而不是包含iframe的页面 如果是这种情况,您需要“代理”“mapguide”内容——即:您的iframe需要从服务器上的另一个
我无法修改javascript函数(页面是用mapguide创建的),因此我希望在iframe之外捕获该调用,将新窗口的内容放入ajax模式框架,而不是打开新窗口,这可能吗?我假设“mapguide”内容来自不同的域,而不是包含iframe的页面 如果是这种情况,您需要“代理”“mapguide”内容——即:您的iframe需要从服务器上的另一个脚本URL(本例中我假设为PHP)加载mapguide内容,服务器的代码将从“mapguide”软件的真正来源获取它。这一部分很简单,服务器端代码可能如下所示:
<?
$content = file_get_contents('http://mapguide.domain/path/to/contents');
// alter content here
print $content;
?>
iframesrc
属性应该指向服务器上包含该代码的PHP文件(而不是“mapguide”服务器)
如果“mapguide”内容包含HTML链接、加载CSS/JavaScript文件或进行AJAX调用,那么您需要让服务器端代码重写这些URL以引用回服务器。这一部分并不容易,实际上取决于“mapguide”JavaScript的复杂程度
因此,在上面的评论说altercontent here
之后,您需要对$content
中包含的HTML进行一些可怕的regexp替换(或解析并重新生成),目的是更改每个URL以通过“代理”PHP脚本,并从“mapguide”服务器上的适当位置加载
如果您成功地完成了所有这些,那么您的iframe将是来自与包含HTML页面相同域的服务器,因此外部页面的JavaScript将能够替换窗口。打开iframe的功能,以防止弹出,或者做任何您想做的事情,正如@jbabey在另一个回答中所说的那样
所有这些都假定“地图指南”内容没有用户协议和/或版权政策,禁止“刮”(自动复制)其(地图指南”内容的作者)内容 虽然我一般不建议这样做,但您可以覆盖窗口的定义。在iframe中打开
函数,假设您的页面和iframe位于同一个域中,以避免XSS安全错误
HTML:
这是我一直在研究的一个类似的剪贴画。。。正确设置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
如果帧来自同一域和端口,您可以重写window.open函数来执行其他您想要的操作。我忘了提到我正在asp.net中制作网页,c#我尝试过这个,但在执行“frame.contentWindow.open”行后,我得到了:“frame.window为null或不是对象”;我在InternetExplorer8上运行这个,这是我的代码: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(“);}@NatyBizz将新函数分配给d.contentWindow.open
,调用d.window.open
时,您缺少一个;
。
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)
};
}
//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