我有一个包含在html iframe中的网页,该网页具有一个javascript函数用于打开链接,该函数使用window.open方法打开一个新窗口。
我无法修改javascript函数(该页面是由mapguide创建的),因此我想在iframe外部捕获该调用,将新窗口的内容放入ajax模态框架中,而不是打开新窗口,这可能吗?
我有一个包含在html iframe中的网页,该网页具有一个javascript函数用于打开链接,该函数使用window.open方法打开一个新窗口。
我无法修改javascript函数(该页面是由mapguide创建的),因此我想在iframe外部捕获该调用,将新窗口的内容放入ajax模态框架中,而不是打开新窗口,这可能吗?
window.open
函数的定义,前提是你的页面和iframe在同一个域中以避免XSS安全错误。<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)
};
}
d.contentWindow.open
赋予新函数后,你漏掉了一个 ;
,而在调用 d.window.open
时,你多加了一个 ;
。 - jbabey<?
$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在另一个答案中所说的那样。
这是我正在处理的类似片段……获取 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