Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
<;脚本>;在javascript代码文档内。write()_Javascript_Internet Explorer - Fatal编程技术网

<;脚本>;在javascript代码文档内。write()

<;脚本>;在javascript代码文档内。write(),javascript,internet-explorer,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-

我在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();
}
但是,即使在Firefox中,代码也不会被加载(使用firebug检查也不会显示它甚至可以看到它)


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

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

IE在打开新窗口时暂停,继续使用100%的CPU。

Google Analytics是这样做的:

var_gat,gaJsHost=((“https:== 文件、位置、协议)? "." : "."); 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();
   }
}

这里callback就是要执行的函数。

这段代码对我来说很有用:

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);
}
您需要在附加的同一文档中创建脚本元素。

请查看

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


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

我没有在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=嗯,您的域。本质上,这在IE中正确设置了地址栏,以便使用
/foo/bar.js
类型src和href可以正确定位和工作


希望这能帮助别人P

旁注:性能提升。使用一个write语句而不是多个write语句。首先构建整个HTML字符串,然后写入窗口。非常感谢您提供的信息。肯定会修好的。
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");
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)