简体   繁体   English

如何删除/停止在jquery中执行脚本

[英]how to remove/stop script from being executed in jquery

i want to make <script> ...... </script> as text(or not executable code) which is returned from another page using ajax. 我想将<script> ...... </script>作为使用ajax从另一个页面返回的文本(或非可执行代码)。

<div id="page"></div>

 $.ajax({
        url: "http://example.com",
        type: "GET",
        crossDomain: true,
        dataType: "text/html",
        success: function(response) {
            console.log(response);
            $('#page').append(response);
        },
        error: function(xhr, status) {
            alert("error");
        }
    });

my problem is that the above code executes the script present inside the response if error occurs my furthur page will not execute and program will stop. 我的问题是上面的代码执行response出现的脚本,如果发生错误,我的页面将不会执行,程序将停止。 so i wanted to make script not executable . 所以我想让script not executable how can i do it? 我该怎么做?

or how can i make any code inside script blank 或者如何code inside script blank创建任何code inside script blank

suppose my script is like this 假设我的脚本是这样的

<script type="text/javascript">
  // some erroneous code or some resource load which failed verification
  console.log('i want to remove every code');
</script>

I think you have to do 3 steps 我想你必须做3个步骤

  1. converting received htmlstring in to DOM element like 将收到的htmlstring转换为DOM元素

      parser = new DOMParser() doc = parser.parseFromString(response, "text/xml"); 

or use 或使用

var doc = document.createElement('div');
doc.innerHTML = response;
  1. Then finding all script tags init 然后找到所有脚本标签init

    allScriptTags = doc.find( "script" )

  2. After that run loop on allScriptTags and remove all script tags 之后在allScriptTags上运行循环并删除所有脚本标记

 allScriptTags.forEach( function(item) { item.remove(); // or item.parentNode.removeChild(item); for older browsers (Edge-) }); 

Finally, 最后,

     $.ajax({
            url: "http://example.com",
            type: "GET",
            crossDomain: true,
            dataType: "text/html",
            success: function(response) {
                console.log(response);
                parser = new DOMParser()
                doc = parser.parseFromString(response, "text/xml");
                allScriptTags = doc.find( "script" )
                allScriptTags.forEach(
                    function(item) {
                        item.remove();
                        // or item.parentNode.removeChild(item); for older browsers (Edge-)
                });
                $('#page').append(doc);
            },
            error: function(xhr, status) {
                alert("error");
            }
        }); 

I hope it will help. 我希望它会有所帮助。

If it doesn't work, try to use string manipulation to remove tags from remove htmlstring. 如果它不起作用,请尝试使用字符串操作从删除htmlstring中删除标记。

Plunker : http://plnkr.co/edit/OUi8U945MIMFZoWQpBGy?p=preview Plunkerhttp//plnkr.co/edit/OUi8U945MIMFZoWQpBGy?p=preview

使用“<pre>”或(html5 support)标记按原样显示代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM