文档.Write - replace "</script>"带有"</script>&q

document.write - replace "</script>" tags with "</script>"

本文关键字:quot script 带有 replace Write 文档      更新时间:2023-09-26

我已经手动测试了这段代码,将反斜杠添加到所有的</script>标记中,并且
如果所有的标签都变成<'/script>,则代码有效。

var iframe = document.createElement('iframe');
var html = '<html><head><script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.js"><'/script><script type="text/javascript">$(window).load(function(){function popo1(){alert("ciaoooo!");}popo1();$(".eccolo").html("<br><br><br><br>xD sygsyusgsuygsus ysg usygsuys");});<'/script></head><body><div class="eccolo"></div></body></html>';
document.body.appendChild(iframe);
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(html);
iframe.contentWindow.document.close();
演示


但我需要动态地自动替换所有的</script>标签与<'/script>使用类似

的东西
XXX.replace(/<'/script>/ig, "<'''/script>");

根据这篇文章


但似乎这种类型的替换实际上不起作用…

var iframe = document.createElement('iframe');
var XXX = '<html><head><script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.js"><'/script><script type="text/javascript">$(window).load(function(){function popo1(){alert("ciaoooo!");}popo1();$(".eccolo").html("<br><br><br><br>xD sygsyusgsuygsus ysg usygsuys");});<'/script></head><body><div class="eccolo"></div></body></html>';
var YYY = XXX.replace(/<'/script>/ig, "<'''/script>");
document.body.appendChild(iframe);
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(YYY);
iframe.contentWindow.document.close();
演示


不幸的是,我不能使用。js文件,所以我希望有一种方法来正确地做标签替换

但是如果我想动态地用<'/script>替换所有的</script>标签…

在下面的评论中,你说:

我从一个总是改变的输入中得到var XXX ..我刚刚在我的问题中添加了一个定义值(var XXX='<html><head>...),例如

这和你的问题完全不同。如果您说您将接收XXX字符串的输入,其内容(在内存中,而不是字符串字面量)看起来像这样:

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.js"></script>
    <script type="text/javascript">
        $(window).load(function() {
            function popo1() {
                alert("ciaoooo!");
            }
            popo1();
            $(".eccolo").html("<br><br><br><br>xD sygsyusgsuygsus ysg usygsuys");
        });
    </script>
</head>
<body>
    <div class="eccolo"></div>
</body>
</html>

…那么这个输入就完全没问题了,可以按原样使用来设置iframe的内容。你不需要在上面做替换。你所链接的帖子与你正在做的事情无关。

但是如果你说你将得到这样的输入:

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.js"></script>
    <script type="text/javascript">
        $(window).load(function() {
            var str = "The problem is here: </script>"; // <======
        });
    </script>
</head>
<body>
    <div class="eccolo"></div>
</body>
</html>

…那么您就处于与HTML解析器相同的不幸境地:您不知道子字符串</script>实际上何时是脚本元素的结尾,或者是JavaScript字符串字面量(或注释)中的文本。如果您有一个包含该内容的网页,HTML解析器将在The problem is here:之后立即得出脚本元素结束的结论。实际上,如果您通过document.write将该内容输出到iframe,解析器就会阻塞它。线:

var str = "The problem is here: </script>";

必须是

var str = "The problem is here: <'/script>";
// or
var str = "The problem is here: </sc" + "ript>";
// or similar

…以避免干扰HTML解析器。(在.js文件中可以,但这不是您的用例。)

从根本上说,如果你接收的输入中包含类似的内容,那么给你输入的人就是在给你无效的输入。子字符串</script> 不能出现在JavaScript代码中的<script>/</script>标签中。不在字符串字面值中,不在注释中,无处可去

规范定义的答案是:不要试图弄清楚它,要求它是正确的。但是如果你知道脚本是JavaScript,并且你真的真的想允许无效的输入并纠正它,你将需要一个JavaScript解析器。这听起来很离谱,但Esprima确实如此,在Meteor的东西中有jsparser,可能还有其他的。您将扫描给定的字符串以找到<script>,然后让JavaScript解析器接管并解析代码(您可能需要修改它,以便它知道在字符串文字/注释之外的</script>中停止)。然后获取解析器使用的文本,使用replace将代码文本中的任何</script>转换为<'/script>,然后继续。

它是不平凡的,这就是为什么规范不要求HTML解析器做它。

但是,再次,如果输入像您的问题中的示例(没有反斜杠,您使用您的字符串字面量来避免此问题),您根本不必做replace。只要把它输出到iframe,它就会工作得很好。

您可以通过编程方式创建script标签,并在页面加载后在head标签中添加。

下面是代码和DEMO
var iframe = document.createElement('iframe');
var html = '<html><head></head><body><div class="eccolo"></div></body></html>';
document.body.appendChild(iframe);
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(html);
var script1 = iframe.contentWindow.document.createElement('script');
var script2 = iframe.contentWindow.document.createElement('script');
script2.textContent = '$(window).load(function(){function popo1(){alert("ciaoooo!");}popo1();$(".eccolo").html("<br><br><br><br>xD sygsyusgsuygsus ysg usygsuys");});'
var head = iframe.contentWindow.document.querySelector('head');
head.appendChild(script1);
script1.onload = function() {
    head.appendChild(script2);    
}
script1.src = 'http://code.jquery.com/jquery-1.11.0.js';
iframe.contentWindow.document.close();