如何在javascript中添加一个div到弹出窗口?

时间:2012-04-10 06:44:24

标签: javascript

我在javascript中使用以下代码创建了一个弹出窗口;

  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

中的查询字符串而变化

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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“

答案 2 :(得分:1)

bergi表示没有从外部域附加到DOM。但对于相同的域名(取自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

相关问题