我把一個網頁放到一個html iframe中,網頁有一個javascript函數來打開鏈接,該函數使用window.open方法打開一個新窗口。從javascript中抓住window.open
我無法修改javascript函數(該頁面是使用mapguide製作的),所以我想在iframe之外捕獲該調用,以將新窗口的內容放入ajax模式框架中,而不是打開一個新窗口, 這可能嗎?
我把一個網頁放到一個html iframe中,網頁有一個javascript函數來打開鏈接,該函數使用window.open方法打開一個新窗口。從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)
};
}
我試過這個,但是在執行'frame.contentWindow.open'這一行後,我得到:'frame。窗口爲空或不是對象';我在Internet Explorer 8上運行這個,這是我的代碼:var d = document.getElementById(「ifr」); //我的框架 if(d){iframe {var.root = d.contentWindow.open; d.contentWindow.open = function(url,windowName,windowFeatures){alert(windowFeatures); return orgOpen(url,windowName,windowFeatures); } d.window.open(「http://stackoverflow.com」); } – 2012-04-04 20:38:33
@NatyBizz你的代碼中有一堆語法錯誤。 – jbabey 2012-04-04 20:55:35
你能否給我更多關於我的錯誤代碼的細節? – 2012-04-04 21:02:53
我假設'地圖指南'的內容是由不同於包含iframe的頁面提供的。
如果是這種情況,您需要「代理」'mapguide'內容 - 即:您的iframe需要從另一個腳本URL(我將假設PHP用於此示例)加載映射指南內容你的服務器,其代碼將從任何真正的來源獲取'mapguide'軟件。這部分是容易的,而服務器端的代碼可能是這樣的:
<?
$content = file_get_contents('http://mapguide.domain/path/to/contents');
// alter content here
print $content;
?>
iframe的src
屬性應該包含的代碼(而不是「的MapGuide」服務器)服務器上的PHP文件指向。
如果「MapGuide的」內容包含HTML鏈接,加載CSS/JavaScript文件,或做AJAX調用,那麼您需要在您的服務器端代碼重寫這些網址指回您的服務器。這部分不是很容易,而且真正取決於'mapguide'JavaScript是多麼複雜。
所以上面的評論說alter content here
之後,你需要做一些可怕的正則表達式替換(或分析和重新生成)包含在$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
如果幀來自相同的域和端口,則可以重寫window.open函數來執行其他所需的操作。 – 2012-04-04 13:24:50
我忘了提及我在asp.net中製作我的網頁,c# – 2012-04-04 20:39:36