简体   繁体   English

从html数据中删除脚本…使用javascript

[英]removing scripts from html data… using javascript

So I have this function to strip out scripts from the page, but some scripts that are many lines long are still showing up. 因此,我具有此功能,可以从页面中删除脚本,但是仍会显示一些行长很多的脚本。 Is there a way to remove all scripts from the page that loads. 有没有一种方法可以从加载的页面中删除所有脚本。

 function filterData(data){

// filter all the nasties out
// no body tags
data = data.replace(/<?\/body[^>]*>/g,'');
// no linebreaks
data = data.replace(/[\r|\n]+/g,'');
// no comments
data = data.replace(/<--[\S\s]*?-->/g,'');
// no noscript blocks
data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g,'');
// no script blocks
data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g,'');
// no self closing scripts
data = data.replace(/<script.*\/>/,'');

// [... add as needed ...]
return data;
  }

Here is an example of the script that comes through in the html 这是html中通过脚本的示例

<script type="text/javascript">
var ccKeywords="keyword=";
if (typeof(ccauds) != 'undefined')
{
 for (var cci = 0; cci < ccauds.Profile.Audiences.Audience.length; cci++)
{
  if (cci > 0) ccKeywords += "&keyword="; ccKeywords +=     ccauds.Profile.Audiences.Audience[cci].abbr;
}
}
</script>

If I got you right, you need to remove all <script> tags with inner code from piece of HTML string. 如果我理解正确,则需要从一段HTML字符串中删除所有带有内部代码的<script>标记。 In this case you can try the following regular expression: 在这种情况下,您可以尝试以下正则表达式:

data.replace(/<script.*?>[\s\S]*?<\/script>/ig, "");

It should successfully work with one-liners and multi-liners, and does not affect other tags. 它应该可以成功地与单线和多线一起使用,并且不会影响其他标签。

DEMO: http://jsfiddle.net/9jBSD/ 演示: http : //jsfiddle.net/9jBSD/

checkout sugar.js - http://sugarjs.com/ 结帐sugar.js- http: //sugarjs.com/

it has a removeTags method that should do what you want 它有一个removeTags方法,该方法应该执行您想要的操作

http://sugarjs.com/api/String/removeTags http://sugarjs.com/api/String/removeTags

function filterData(data){
    var root = document.createElement("body");
    root.innerHTML = data;

    $(root).find("script,noscript").remove();

    function removeAttrs( node ) {
        $.each( node.attributes, function( index, attr ) {
            if( attr.name.toLowerCase().indexOf("on") === 0 ) {
                node.removeAttribute(attr.name);
            }
        });
    }

    function walk( root ) {
        removeAttrs(root);
        $( root.childNodes ).each( function() {
            if( this.nodeType === 3 ) {
                if( !$.trim( this.nodeValue ).length ) {
                    $(this).remove();
                }
            }
            else if( this.nodeType === 8 ) {
                $(this).remove();
            }
            else if( this.nodeType === 1 ) {
                walk(this);
            }
        });
    }

    walk(root);

    return root.innerHTML; 
}

filterData("<script>alert('hello');</script></noscript></script><div onclick='alert'>hello</div>\n\n<!-- comment -->");
//"<div>hello</div>"

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

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