Append html to jQuery element without running scripts inside the html

后端 未结 2 2048
别跟我提以往
别跟我提以往 2021-02-02 15:46

I have written some code that takes a string of html and cleans away any ugly HTML from it using jQuery (see an early prototype in this SO question). It works pretty well, but I

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-02 16:27

    You should remove the script elements:

    var wrapper = $('
    ').append($(html).remove("script"));

    Second attempt:

    node-validator can be used in the browser: https://github.com/chriso/node-validator

    var str = sanitize(large_input_str).xss();
    

    Alternatively, PHPJS has a strip_tags function (regex/evil based): http://phpjs.org/functions/strip_tags:535

提交回复
热议问题