【发布时间】:2010-04-23 06:36:49
【问题描述】:
我用 JavaScript 打开了一个新窗口:
var newwin = window.open('','preview','width=600,height=500');
现在我想在窗口中写一些 JavaScript:
newwin.document.write("<script type='text/javascript'>alert('Hi!');<" + "/script>");
newwin.document.close();
但是,脚本永远不会被执行。我做错了吗?
更新:好的,现在我已经完成了一部分工作。这是我当前的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type='text/javascript'>
function SetContent()
{
$('#content').html('New Text');
}
function dowin()
{
var newwin = window.open('','preview','width=600,height=500');
newwin.document.write('<div id="content"></div><b>This should be replaced...</b>');
newwin.document.close();
var script = newwin.document.createElement("script");
script.type = "text/javascript";
script.src = 'jquery.js';
newwin.document.body.appendChild(script);
var script2 = newwin.document.createElement("script");
script2.type = "text/javascript";
script2.textContent = "(" + SetContent + ")();";
newwin.document.body.appendChild(script2);
}
</script>
</head>
<body>
<button onclick='dowin();'>Open</button>
</body>
</html>
它应该改变div 的内容,但正如你所见,它没有。
【问题讨论】:
-
实际上,您的代码在我的 Firefox 3.7 上运行良好,但您也可以尝试我的方法。
-
顺便说一下,Chrome 的检查器是无用的,因为它被禁用弹出窗口:(
标签: javascript window