2012-04-04 3 views
2

웹 페이지에 HTML iframe이 있고, 웹 페이지에 링크를 여는 자바 스크립트 기능이 있으며,이 함수는 window.open 메서드를 사용하여 새 창을 엽니 다.자바 스크립트에서 window.open을 잡아

새 페이지의 내용을 새 창을 여는 대신 아약스 모달 프레임에 넣으려면 iframe 외부에서 호출을 catch하고 싶습니다 (페이지가 mapguide로 작성된) JavaScript 함수를 수정할 수 없습니다 , 이것이 가능한가?

+0

프레임이 동일한 도메인 및 포트에서 경우에, 당신은 당신이 원하는 뭔가 다른 작업을 수행 할 수 window.open 함수를 재정의 할 수 있습니다. –

+0

나는 asp.net, C# –

답변

3

일반에서는 권장하지 않지만 XSS 보안 오류를 피하기 위해 페이지와 iframe이 동일한 도메인에 있다고 가정하면 window.open 함수의 정의를 iframe에 덮어 쓸 수 있습니다.

HTML : 부모 창에서

<iframe id="myFrame" src="..."> 
</iframe> 

자바 스크립트 : 여기

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) 
    }; 
} 
+0

내 웹 페이지를 만들고있어 언급 깜빡했는데 'frame.contentWindow.open'줄을 실행 한 후 '프레임.window가 null이거나 객체가 아닙니다. '; 나는 Internet Explorer 8에서이 코드를 실행하고있다. var d = document.getElementById ("ifr"); // 내 프레임 if (d) { var ifropen = d.contentWindow.open; d.contentWindow.open = function (url, windowName, windowFeatures) { 경고 (windowFeatures); 반환 orgOpen (url, windowName, windowFeatures); } d.window.open ("http://stackoverflow.com"); } –

+0

@NatyBizz 코드에 구문 오류가 많이 있습니다. – jbabey

+0

내 잘못된 코드에 대해 자세히 설명해 주시겠습니까? –

0

'mapguide'콘텐츠가 iframe이 포함 된 페이지가 아닌 다른 도메인에서 제공된다고 가정합니다.

이 경우 'mapguide'콘텐츠를 '프록시'해야합니다. 즉, iframe에서 다른 스크립트 URL의 mapguide 콘텐츠를로드해야합니다 (이 예의 경우 PHP로 가정). 귀하의 서버는 코드가 실제로 어디서 왔는지에 따라 '지도 가이드'소프트웨어를 가져옵니다. 이 부분은 용이하고, 서버 측 코드는 다음과 같습니다

<? 
    $content = file_get_contents('http://mapguide.domain/path/to/contents'); 

    // alter content here 

    print $content; 
?> 

iframe을 src 속성은 그 코드 (대신 'MapGuide를'서버)를 포함하여 서버의 PHP 파일로 가리켜 야합니다.

'mapguide'콘텐츠에 HTML 링크가 포함되어 있거나 CSS/JavaScript 파일을로드하거나 AJAX 호출이있는 경우 서버 측 코드에서 서버를 다시 참조하도록 해당 URL을 다시 작성해야합니다. 그 부분은 그리 쉽지는 않으며 실제로 '지도 안내'JavaScript가 얼마나 복잡한 지에 따라 달라집니다. 그 위의 댓글이 alter content here을 말한다 후

그래서, 당신은 통과 할 때마다 하나의 URL을 변경하는 것을 목표로, 몇 가지 끔찍한 정규 표현식 교체 할 (또는 구문 분석하고 다시 생성) 할 $content에 포함 된 HTML을 필요 했어 당신의 "프록시"PHP 스크립트와 'mapguide'서버의 적절한 위치에서로드 할 수 있습니다.

모든 기능을 사용하지 않으면 iframe이 포함 된 HTML 페이지와 동일한 도메인의 서버가되어 외부 페이지의 JavaScript가 iframe의 window.open 함수를 바꿀 수있게됩니다. @jbabey가 다른 답변에서 말했듯이, 팝업을 사용하거나 원하는 것을 대신 할 수 있습니다.

이 모든 것은 'mapguide'콘텐츠가 'mapguide'콘텐츠의 작성자 '콘텐츠를'긁어 내기 '(자동 복사)하는 것을 금지하는 사용자 동의 및/또는 저작권 정책을 따르지 않는다고 가정합니다.

0

나는 ...는 contentWindow의 권리를 점점 교묘했다 유사한 SNIPPIT 작업했습니다,하지만 어쩌면이 통찰력을 제공합니까?

//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 
관련 문제