我已經打開了新的窗口使用JavaScript:如何將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 btw上運行良好,但你也可以嘗試我的方法。 – YOU 2010-04-23 06:53:05
順便說一句,Chrome的檢查器是無用的,因爲它被禁用彈出窗口:( – 2010-04-23 21:27:42