我把一个网页转换成html iframe,这个网页有一个javascript函数来打开链接,这个函数用window.open方法打开一个新窗口。
我不能修改javascript函数(页面是用mapguide制作的),所以我想捕捉iframe外部的调用,将新窗口的内容放到ajax模式框架中,而不是打开一个新窗口,这可能吗?
发布于 2012-04-04 21:38:26
虽然我一般不建议这样做,但您可以覆盖iframe中的window.open函数的定义,假设您的页面和iframe在同一个域中,以避免出现安全错误。
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)
};
}发布于 2012-04-04 21:37:52
我假设'mapguide‘内容来自不同的域,而不是包含iframe的页面。
如果是这种情况,你需要“代理”“mapguide”内容--也就是说:你的iframe需要从你服务器上的另一个脚本URL (本例中假设是PHP )加载mapguide内容,该URL的代码将从“mapguide”软件的实际来源获取它。这一部分很简单,服务器端代码可能如下所示:
<?
$content = file_get_contents('http://mapguide.domain/path/to/contents');
// alter content here
print $content;
?>iframe属性应该指向服务器上包含该代码的src文件(而不是“mapguide”服务器)。
如果“mapguide”内容包含HTML链接、加载CSS/JavaScript文件或执行AJAX调用,则需要让服务器端代码重写这些URL以引用回服务器。这部分并不是很容易,实际上取决于“mapguide”JavaScript的复杂程度。
因此,在上面提到alter content here的注释之后,您需要对包含在$content中的超文本标记语言做一些糟糕的regexp替换(或解析和重新生成),其目标是更改每个单独的URL,使其通过您的“代理”PHP脚本,并从'mapguide‘服务器上的适当位置加载。
如果您设法完成了所有这些操作,那么您iframe将与包含的HTML页面位于同一个域中,因此外部页面的JavaScript将能够替换iframe的window.open函数--以防止弹出,或者做您想做的任何事情--正如@jbabey在另一个答案中所说的那样。
所有这些都假定'mapguide‘内容没有用户协议和/或版权政策,后者禁止“抓取”(自动复制)其( 'mapguide’内容的作者)内容。
发布于 2014-06-14 00:31:59
这是我一直在做的一个类似的片段...很难让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 definitionhttps://stackoverflow.com/questions/10011986
复制相似问题