<脚本>里面的javascript代码document.write() [英] <script> inside javascript code document.write()

查看:93
本文介绍了<脚本>里面的javascript代码document.write()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了嵌入HTML(在服务器端生成)的一部分javascript代码,如下所示:

I got a portion of javascript code embedded in HTML (generated on the server side) that looks like this:

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

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

This code gets executed on click of a element.

对我来说有问题的部分是包含javascript文件 - 它在Firefox和Chrome中运行正常,但IE(7和8,正如我测试的)表现得很奇怪。在那里包含 JSFILE 的行,点击窗口打开,但是为空,CPU 100%忙,唯一的办法是杀死IE。

The problematic part for me is the inclusion of javascript file - it works ok in Firefox and Chrome, but IE (7 and 8, as I tested) behaves strange. With the line containing JSFILE there, the window on click gets opened, but is empty, CPU is 100% busy and only way is to kill IE.

任何人都可以帮忙解决这个问题吗?也许我应该用其他方式在那里插入javascript文件?

Anyone can help with handling this problem? Maybe I should use some other way to insert the javascript files in there?

我试过,而不是 win.document.write(),DOM操作方法,把这个部分 win.document.close()之后的代码:

I tried, instead of win.document.write(), the DOM-manipulation method, putting this part of code after win.document.close():

h = win.document.getElementsByName('head')[0];
js = document.createElement('script');
js.src = '/js/JSFILE.js';
h.appendChild(js);

然后代码未加载,即使在Firefox中也是如此(并且使用firebug进行检查时未显示它甚至可以看到它)。

but then the code isn't loaded, even in Firefox (and inspecting with firebug doesn't show it even can see it).

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

After some checks, I found out that the problem is caused by <script> elements with a src= attribute defined. If I add an inline script, like:

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

,窗口打开,警报框出现,一切都很好。

within my document.write(), the window opens, the alert box shows up and everything's all right.

但是使用

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

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

IE stalls when opening the new window, keeps using 100% of 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);
}

以下是我需要更改代码才能使其正常工作:

Here is what I needed to change in your code to make it work:

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

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

You need to create the script element in the same document that you are appending.

这篇关于&LT;脚本&GT;里面的javascript代码document.write()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆