JS能否打开一个空的HTML弹出窗口?

3
所以我们可以这样做:
<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>

但是如何在HTML页面的头部添加像<script type="text/javascript" src="path-to-MathJax/MathJax.js"></script>这样的内容,以启用MathML和TeX公式的渲染呢?


2
不要在onsomething处理程序中放置javascript:... - ThiefMaster
3个回答

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>');

空白的window.open URL参数会不会在IE浏览器上破坏同源策略 - Roberto Linares
@RobertoLinares:我不确定,但我在IE9中测试过,没有任何问题。 - Marcel

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>

抱歉,当我完成我的演示时,我发现你不能使用document.write。 - TimonWang

1
你可以考虑使用 iframe,你可以将其 head 和 body 设置在一起,然后将其放置在窗口中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接