JavaScript如何创建包含更多JavaScript的新页面? [英] How can JavaScript make new page that contains more JavaScript?

查看:169
本文介绍了JavaScript如何创建包含更多JavaScript的新页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开使用JS的新页面并将它写入HTML代码,但是当我尝试使用 document.write()函数在新页面中编写JS时它不起作用。显然,主JS在它看到< / script> 时会立即关闭,它将用于打开新页面的JS。



有没有围绕这个?

  var open = window.open(); 
opened.document.write(<!DOCTYPE html PUBLIC \ - // W3C // DTD XHTML 1.0 Transitional // EN \\http://www.w3.org/TR /xhtml1/DTD/xhtml1-transitional.dtd\\">);
opens.document.write(< html lang = \en-US\xml:lang = \en-US\xmlns = \http://www.w3 .ORG / 1999 / xhtml\ > 中);
opened.document.write(< head>< title>打印副本< / title>< / head>);
opened.document.write(< body>);
opened.document.write(< script type = \text / javascript\>);
opened.document.write(< / script>);
opens.document.write(< p> test< / p>< / body>< / html>);


解决方案

这听起来像是将JS嵌入HTML文档而不是使用外部文件,并且您将其作为文本/ html提供。 (如果是XHTML,那么你会得到一个简单的格式错误)。

你可以将代码移动到一个外部文件中(这是最简单的选择),或者您可以在看起来像结束标签的任何内容上跳过斜线字符:

  document.write(< \ / script> ;); 

请注意,您应该对所有结束标签(而不是<$由于SGML的工作方式(即使大多数浏览器不完全遵守SGML的规则),也是如此。

关于此主题的常见问题解答条目

I'm opening new page using JS and writing into it the HTML code, however when I try to write JS inside the new page using document.write() function it doesn't work. Apparently the main JS closes as soon as the it sees </script> which is intended for the JS of the new page that will be opened.

Is there away around this?

var opened = window.open("");
opened.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
opened.document.write("<html lang=\"en-US\" xml:lang=\"en-US\" xmlns=\"http://www.w3.org/1999/xhtml\">");
opened.document.write("<head><title>Print Copy</title></head>");
opened.document.write("<body>");
opened.document.write("<script type=\"text/javascript\">");
opened.document.write("</script>");
opened.document.write("<p>test</p></body></html>");

解决方案

This sounds like you are embedding the JS into an HTML document rather than using an external file, and that you are serving it as text/html. (If it was XHTML then you would get a simple well-formedness error).

You could move the code to an external file (which is the simplest option), or you can escape the slash characters on anything that looks like an end tag:

document.write("<\/script>");

Note you should do this to all end tags (not just </script>) due to the way SGML works (even if most browsers don't completely respect the rules of SGML).

There is a good FAQ entry on this subject.

这篇关于JavaScript如何创建包含更多JavaScript的新页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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