Javascript document.write-替换“文件”</脚本>&引用;带“的标签<\/脚本>&引用;

Javascript document.write-替换“文件”</脚本>&引用;带“的标签<\/脚本>&引用;,javascript,jquery,replace,Javascript,Jquery,Replace,我已经手动测试了这段代码,将反斜杠添加到所有的标记中,并且 如果所有的标签都变成,代码就会工作 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="tex

我已经手动测试了这段代码,将反斜杠添加到所有的
标记中,并且
如果所有的标签都变成
,代码就会工作

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();
var-iframe=document.createElement('iframe');
var html='$(window.load(function(){function popop1(){alert(“ciaooooo!”);}popo1();$(“.eccolo”).html(“



xD sygsyusgsuygsus ysg usygsuys”);}); document.body.appendChild(iframe); iframe.contentWindow.document.open(); iframe.contentWindow.document.write(html); iframe.contentWindow.document.close();


但是我需要使用类似的方法动态地将所有
标记替换为

XXX.replace(/<\/script>/ig, "<\\\/script>");
XXX.替换(//ig,”);
根据


但似乎这种替换实际上不起作用

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();
var-iframe=document.createElement('iframe');
var XXX='$(window.load(function(){function popo1(){alert(“ciaooooo!”);}popo1();$(“.eccolo”).html(“



xD sygsyusgsuygsu ysg usygsu”);}); var YYY=XXX。替换(//ig,“”); document.body.appendChild(iframe); iframe.contentWindow.document.open(); iframe.contentWindow.document.write(YYY); iframe.contentWindow.document.close();


不幸的是,我不能使用.js文件,所以我希望有一种方法可以正确地替换标记

但是如果我想用
动态替换所有
标记,该怎么办呢

在下面的评论中,你说:

我从一个总是变化的输入中获取
var XXX
。。我只是在我的问题中添加了一个定义值(
var XXX=”…
),举个例子

这和你的问题完全不同。如果您说您将收到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>
<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解析器处于同样不幸的位置:您不知道子字符串
何时实际结束脚本元素,或者是JavaScript字符串文本(或注释)中的文本。如果您有一个包含该内容的网页,HTML解析器将得出结论,脚本元素在
出现问题后立即结束:
。事实上,如果您通过
document.write
将该内容输出到
iframe
,解析器将阻塞它。该行:

var str = "The problem is here: </script>";
var str=“问题就在这里:”;
需要

var str = "The problem is here: <\/script>";
// or
var str = "The problem is here: </sc" + "ript>";
// or similar
var str=“问题就在这里:”;
//或
var str=“问题就在这里:”;
//或类似
…以避免绊倒HTML解析器。(在
.js
文件中可以,但这不是您的用例。)

从根本上说,如果你收到的输入中包含类似的内容,那么给你的人就是在给你无效的输入。子字符串
不能出现在
/
标记中的JavaScript代码中,不能出现在字符串文本中,也不能出现在注释中

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

这是非常重要的,这就是规范不需要HTML解析器的原因


但是,同样,如果输入与问题中的示例类似(没有使用反斜杠来避免字符串文字出现此问题),则根本不必执行
替换。只需将它输出到iframe,它就可以正常工作。

您可以通过编程创建脚本标记,并在加载页面后附加到head标记中

以下是代码和

var-iframe=document.createElement('iframe');
var 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 popopo1(){alert(“ciaooo!”);}popo1();$(.eccolo”).html(“



xD sygsyusgsuygsu ysg usygsuys”);” var head=iframe.contentWindow.document.querySelector('head'); 头、子(脚本1); script1.onload=函数(){ 头、子(脚本2); } script1.src=http://code.jquery.com/jquery-1.11.0.js'; iframe.contentWindow.document.close();

希望它能帮助你……

你为什么要使用
文档。打开/写/关闭
。@爱德华:我已经更新了我的答案。那篇文章中的内容与你正在做的事情无关。你为什么认为它不起作用?在演示中尝试
alert(YYY)
。它有
如您所愿如果您必须在var XXX中包含脚本,并且不能使用.js文件,那么您需要在
XXX
变量进入脚本/HTML之前添加反斜杠。
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();