ken*_*der 9 javascript internet-explorer
我得到了一些嵌入HTML(在服务器端生成)的javascript代码,如下所示:
function winWriteMail2(){
var win = open('','wininfo', 'width=400,height=300,scrollbars=yes,resizable=yes');
win.document.open();
win.document.write('<HTML><HEAD><META http-equiv="Content-type" content="text/html; charset=iso-8859-2"><LINK rel="stylesheet" type="text/css" href="/css/main.css">');
win.document.write('<scr' + 'ipt language="javascript" type="text/javascript" src="/js/JSFILE.js"></scr' + 'ipt>');
win.document.write('</HEAD><BODY BGCOLOR="#f7f3e7">');
<!-- window content goes here -->
win.document.write('</BODY></HTML>');
win.document.close();
}
Run Code Online (Sandbox Code Playgroud)
此代码在单击元素时执行.
对我来说有问题的部分是包含javascript文件 - 它在Firefox和Chrome中运行正常,但IE(7和8,正如我测试的)表现得很奇怪.随着包含JSFILE那里的行,点击窗口打开,但是为空,CPU 100%忙,唯一的办法是杀死IE.
任何人都可以帮助处理这个问题?也许我应该用其他方式在那里插入javascript文件?
我试过,而不是win.document.write()DOM操作方法,把这部分代码放在win.document.close():
h = win.document.getElementsByName('head')[0];
js = document.createElement('script');
js.src = '/js/JSFILE.js';
h.appendChild(js);
Run Code Online (Sandbox Code Playgroud)
但是后来代码没有被加载,即使在Firefox中(并且用firebug进行检查也没有显示它甚至可以看到它).
经过一些检查后,我发现问题是由定义<script>了src=属性的元素引起的.如果我添加内联脚本,例如:
<script type='text/javascript'>alert('foo')</script>
Run Code Online (Sandbox Code Playgroud)
在我document.write()的窗口打开,警报框出现,一切都很好.
但是使用了
<script type='text/javascript' src='/js/foo.js'></script>
Run Code Online (Sandbox Code Playgroud)
IE在打开新窗口时停止,继续使用100%的CPU.
这段代码对我有用:
function winWriteMail2(){
var win = open('','wininfo', 'width=400,height=300,scrollbars=yes,resizable=yes');
win.document.open();
win.document.write('<HTML><HEAD><META http-equiv="Content-type" content="text/html; charset=iso-8859-2"><LINK rel="stylesheet" type="text/css" href="/css/main.css">');
win.document.write('</HEAD><BODY BGCOLOR="#f7f3e7">');
win.document.write('this is the body content');
win.document.write('</BODY></HTML>');
win.document.close();
var h = win.document.getElementsByTagName("head")[0];
var js = win.document.createElement("script");
js.type = "text/javascript";
js.src = "js/scriptfile.js";
h.appendChild(js);
}
Run Code Online (Sandbox Code Playgroud)
以下是我需要在代码中更改以使其工作的内容:
//From
var js = document.createElement("script");
//To
var js = win.document.createElement("script");
Run Code Online (Sandbox Code Playgroud)
您需要在要追加的同一文档中创建脚本元素.
Google Analytics就是这样做的:
var _gat,gaJsHost =(("https:"== document.location.protocol)?" https:// ssl.":" http:// www."); document.write(unescape("%3Cscript src ='"+ gaJsHost +"google-analytics.com/ga.js'type ='text/javascript'%3E%3C/script%3E"));
| 归档时间: |
|
| 查看次数: |
14085 次 |
| 最近记录: |