繁体   English   中英

javascript代码document.write()里面的<script>

[英]<script> inside javascript code document.write()

我得到了一些嵌入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();
}

此代码在单击元素时执行。

对我来说有问题的部分是包含javascript文件 - 它在Firefox和Chrome中运行正常,但IE(7和8,正如我测试的)表现得很奇怪。 在包含JSFILE的行中, 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);

但是后来代码没有被加载,即使在Firefox中(并且用firebug进行检查也没有显示它甚至可以看到它)。


经过一些检查后,我发现问题是由定义了src=属性的<script>元素引起的。 如果我添加内联脚本,例如:

<script type='text/javascript'>alert('foo')</script>

在我的document.write() ,窗口打开,警告框出现,一切都很好。

但是使用了

<script type='text/javascript' src='/js/foo.js'></script>

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

以下是我需要在代码中更改以使其工作的内容:

//From
var js = document.createElement("script");
//To
var js = win.document.createElement("script");

您需要在要追加的同一文档中创建脚本元素。

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

认为基于DOM的代码很好,但尝试(a)使用绝对脚本URL,(b)设置脚本类型和(c)在追加后更新src,这应该使它更可靠地工作:

var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.type = "text/javascript";
head.appendChild(script);
script.src = "http://host.tld/js/JSFILE.js";

希望这可以帮助。

编辑

顺便说一句,最好设置一种回调,以确保在使用代码之前加载脚本。 代码看起来与此类似:

// most browsers
script.onload = callback;
// IE
script.onreadystatechange = function() {
   if(this.readyState == "loaded"  || this.readyState == "complete") {
      callback();
   }
}

回调就是执行的函数。

看看writeCapture.js

用于协助Ajax加载包含使用document.write的脚本标记的HTML的实用程序

我知道这是一个4岁的线程,但我想通过几个不同的文章/问题添加一个我刚刚合并的修复:

而不是在document.write调用之后附加文档(.css,.js等),我改变了我的open调用,看起来像:

var win = window.open("//"+document.domain, "_blank");//document.domain is the fix
if(win != null)
    win.document.write('...');//insert your content, wherever you got it (hand-coded, ajax, etc)

添加'//'会自动设置协议(http vs https)和document.domain = well,即您的域名。 本质上,这在IE中正确设置地址栏,以便使用/foo/bar.js类型src和href将被定位并正常工作。

希望这有助于某人! :P

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM