Catch the window.open from javascript
Catch the window.open from javascript
我有一个网页变成了一个html iframe,这个网页有一个javascript函数来打开链接,这个函数使用window.open方法来打开一个新窗口。
我无法修改javascript函数(页面是用mapguide创建的),所以我想在iframe之外捕获该调用,将新窗口的内容放入ajax模式框架中,而不是打开一个新窗口,这可能吗?
虽然我一般不建议这样做,但您可以覆盖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)
};
}
我假设"mapguide"内容来自与包含iframe的页面不同的域。
如果是这样的话,你需要"代理"mapguide"内容——也就是说:你的iframe需要从服务器上的另一个脚本URL(我假设这个例子是PHP)加载mapguide内容,该脚本的代码会从任何地方获取"mapguid"软件。这部分很简单,服务器端代码可能如下所示:
<?
$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以引用回您的服务器。这部分并不容易,实际上取决于"地图指南"JavaScript的复杂程度
因此,在上面的评论说alter content here
之后,您需要对$content
中包含的HTML进行一些糟糕的regexp替换(或解析并重新生成),目的是更改每个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
- Calling VB.Net from Javascript
- AWSELB Cookie access from Javascript
- Routing from Javascript lib in Angular JS 2
- Testing .NET Web API from JavaScript
- ASP.net Identity send AntiForgeryToken from javascript
- URI escape / unescape from Javascript to Perl
- Calling Java Web API from Javascript
- ASMX Web Service from Javascript
- Accessing Amazon RDS from javascript
- writing pdf from javascript/cordova
- Read an MVC @Html.TextBoxFor from Javascript
- SelectOneMenu disable from javascript
- innerHTML from Javascript Array
- Calling Swift from JavaScript
- javascript getElementsByClassName from javascript variable
- Accessing TaublarData from javascript
- Catch the window.open from javascript
- Call python from javascript
- Calling Servlet from JavaScript
- Calling Asp Validator from Javascript