2011-01-31 94 views
2

我有一個母版頁,其中jvscrpt函數稱爲openWin(),它打開一個新窗口。 這裏是我的代碼:「我點擊」JavaScript呈現HTML頁面仍在加載

<html> 
<head> 
<script type="text/javascript"> 
function openWin() 
{ 
win=window.open(); 
win.document.write("<html>"); 
win.document.write("<head>"); 
win.document.write("<style type=\"text/css\">"); 
win.document.write("@media print{.input {display:none}}"); 
win.document.write("</style>"); 
win.document.write("</head>"); 
win.document.write("<body>"); 
win.document.write("<table align=\"center\">"); 
win.document.write("<tr><td>result:</td><td>100,--€</td></tr>"); 
win.document.write("<tr><td colspan=\"2\" id=\"idcko\"><input type=\"button\" value=\"click\" class=\"input\" onclick=\"window.print();\"/></td></tr>"); 
win.document.write("</table>"); 
win.document.write("</body>"); 
win.document.write("</html>"); 
} 
</script> 
</head> 
<body> 
<form> 
<input type="button" value="Click me!" onclick="openWin();" /> 
</form> 
</body> 
</html> 

當我點擊按鈕會出現一個新窗口,但瀏覽器無法停止加載頁面。該頁面具有全部功能,但是例如,當我想在Mozilla中查看源代碼時,我只能看到一個空白頁面。 請幫助...

+1

它與打開一個新窗口無關,它只是在文件已經完成寫入時使用document.write了。如果你在當前頁面上執行document.write('test')(加載後),你會得到同樣的效果。他們是saem的原因是因爲window.open()將打開一個完整但空的文檔的新頁面。我建議嘗試使用DOM功能來添加您的HTML元素,應該更好一點。 – Chris 2011-01-31 12:38:19

回答

6

呼叫

win.document.close(); 

末(最後一次寫入後())

這標誌着該寫過程結束瀏覽器和文檔的完整性。

+0

工作正常,非常感謝 – tomasBULL 2011-01-31 12:42:48

1

但例如當我想看看在Mozilla 源代碼,我只得到一個 空白頁。

這是因爲源代碼是由您的javascript編寫的 - 這與AJAX相同(您無法查看源代碼中的更改)。

也許你會更好的打開一個新頁面,並通過GET/POST或服務器端傳遞它需要的參數。