2012-04-10 77 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也。