2012-04-10 43 views
2

我已經在javascript中使用以下代碼創建了一個彈出窗口;如何在JavaScript中添加div到彈出窗口?

window.open("http://google.com", "myWindow", "status = 1, height = 300, width = 300, resizable = 0"); 

我需要添加以下動態div到該彈出窗口。

<div><img src="/images/img1.jpg" /></div> 

上面的DIV是動態的,因爲,圖像SRC將根據該URL

回答

7

您可以查詢字符串沒有改變出於安全原因。由於同源策略(並且google.com當然不是您的域名),您將不被允許訪問其他窗口的DOM。

如果彈出是從同一個域的window.open返回值將是彈出窗口的window對象的引用:https://developer.mozilla.org/en/DOM/window.open

var popup = window.open("/relative path.html", ...); 
popup.onload = function() { // of course you can use other onload-techniques 
    jQuery(popup.document.body).append("<div />"); // or any other dom manipulation 
} 
+0

我可以使用javascript從我的網站添加/刪除內容 – 2012-04-10 06:48:45

+0

是的;我已經延長了答案。 – Bergi 2012-04-10 06:56:20

+0

我對我的問題嘗試了同樣的事情,但沒有奏效。 – 2014-11-19 04:27:15

1

只需使用下面的代碼:

A HREF =」 www.google.com「onclick =」window.open(this.href,null,'height = 580,width = 680,toolbar = 0,location = 0,status = 1,scrollbars = 1,resizable = 1');返回false「

1

由於bergi說不從外延附加到DOM rnal域。但對於同一域(here拍攝)

win=window.open('about:blank','instructions','width=300,height=200'); 
doc=win.document; 
doc.open(); 
doc.write('<div id="instructions">instructions</div>'); 
doc.close(); 
//reference to the div inside the popup 
//->doc.getElementById('instructions') 

你可以看到一個例子here也。