How can JavaScript make new page that contains more JavaScript?

天大地大妈咪最大 提交于 2019-11-28 14:10:53

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.

Daniel Vassallo

If you do not fancy splitting the string, you can also use the escape character:

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

EDIT: as others pointed out, you should escape the slash with a backslash.

The easy (but non standard) solution is to break your string:

opened.document.write("</scr"+"ipt>");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!