<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Javascript</title>
<script type="text/javascript">
document.write("<script type="text/javascript" src="external.js">" + "</scr" + "ipt>");
</script>
</head>
<body>
<input type="button" value="OpenWin" onClick="openWin()">
</body>
</html>
注意:字符串"<script>"被分成了两部分,这是必要的,因为每当浏览器遇到<script>,它都假定其中的代码块是完整的(即使它出现在JavaScript字符串中)。
下面是外部文件external.js的内容:
function openWin() { var oNewWin = window.open("about:blank","newwindow","height:150,width:300,top:10,left:10,resizeable=yes"); oNewWin.document.open(); oNewWin.document.write("<html><head><title>New Window</title></head>"); oNewWin.document.write("<body>This is a new window</body></html>"); oNewWin.document.close(); }这个例子打开空白页,然后写入新页面,在write()前,先调用open()方法,最后调用close()方法完成显示。当你想显示无需返回服务器的页面时,这种方法非常有用。