2011-03-03 27 views
3

因此,我們可以這樣做:是否有可能在JS中打開空的HTML彈出窗口?

<html> 
<head> 
<title>JavaScript Popup Example 3</title> 
</head> 

<script type="text/javascript"> 
function exitpop() 
{ 
    my_window = window.open("", "mywindow1", "status=1,width=350,height=150"); 
    //my_window.document.write('somehow add JS'); 
    my_window.document.write('<h1>Popup Test!</h1><br/>'); 
    my_window.document.write('J_\alpha(x) = \sum_{m=0}^\infty \frac{(-1)^m}{m! \, \Gamma(m + \alpha + 1)}{\left({\frac{x}{2}}\right)}^{2 m + \alpha} '); 
} 
</script> 

<body onunload="javascript: exitpop()" > 
<h1>JavaScript Popup Example 4</h1> 
</body> 
</html> 

但如何,例如添加到標題的東西,如<script type="text/javascript" src="path-to-MathJax/MathJax.js"></script>,使MathML和TEX公式渲染該HTML頁面的?

+2

不要把'javascript:'放在一些處理程序中...... – ThiefMaster 2011-03-03 08:20:45

回答

1

使用javascript創建一個名爲script的節點,並使用javascript設置它的屬性。然後將該節點追加到document.head。

<html> 
<head> 
<title>JavaScript Popup Example 3</title> 
</head> 

<script type="text/javascript"> 
function exitpop() 
{ 
    var my_window = window.open("", "mywindow1", "status=1,width=350,height=150"); 


    var head = my_window.document.getElementsByTagName("head").item(0); 
    alert(head.innerHTML); 
     var script = my_window.document.createElement ("script"); 
     script.src = "a.js"; 
     head.appendChild (script); 
    alert(head.innerHTML); 
} 
</script> 

<body onunload="javascript: exitpop()" > 
<h1>JavaScript Popup Example 4</h1> 
</body> 
</html> 
+0

請問你能提供一個例子嗎? (code) – Rella 2011-03-03 08:31:18

+0

對不起,當我完成演示時,我發現你不能使用document.write。 – TimonWang 2011-03-03 08:48:34

3

是的,你只需要連接的腳本標記,像這樣:jsfiddle demo

錢拍代碼示例是:

jswin = window.open("", "jswin", "width=350,height=150"); 
jswin.document.write("Here's your popup!"); 
jswin.document.write('<scr'+'ipt>alert("Here\'s your alert()!");</scr'+'ipt>'); 
+0

空白的window.open URL參數不會在IE瀏覽器上破壞[同源策略](http://en.wikipedia.org/wiki/Same-origin_policy)嗎? – 2014-07-24 17:26:04

+0

@RobertoLinares:我不確定,但我在IE9中測試了這個沒有任何問題。 – Marcel 2014-07-24 23:31:19

1

你可以考慮使用iframe,你可以將頭部和身體完全放在一起,然後放在窗戶中。

相關問題