Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use JQuery to remove all "script" tags in a string of HTML?

Suppose I have a string of HTML code. I want to use JQuery to remove all <script> tags from the string.

How can I do that?

Note: I want to use JQuery , not REGEX, to do this.

Does this work? $(var).find('script').remove();

like image 604
user847495 Avatar asked Dec 14 '11 11:12

user847495


People also ask

How do I remove a script in HTML?

Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.

How do I remove a specific script tag in JQuery?

You can use . filter() to get the script with src as 'http://firstScript.com' and the . remove() .

How can you remove and HTML element using JQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.

What does JQuery remove do?

jQuery remove() Method The remove() method removes the selected elements, including all text and child nodes. This method also removes data and events of the selected elements. Tip: To remove the elements without removing data and events, use the detach() method instead.


2 Answers

This should work for you:

var stringOfHtml = // your string here
$(stringOfHtml).find('script').remove();

To get the new string with the script tags removed:

var stringOfHtml = "<div><script></script><span></span></div>";
var html = $(stringOfHtml);
html.find('script').remove();

var stringWithoutScripts = html.wrap("<div>").parent().html(); // have to wrap for html to get the outer element

JS Fiddle Example - Had to use p instead of script as script broke the fiddle, same principle though.

Actual working answer here (hopefully)

Here is a workaround for the script issue, use replace to swap the script text with something else (try and make it unique) then remove those new tags and use replace again to swap back in case script is used anywhere else in text. Yes, it does use regex, but not to remove the script tags so I'm hoping that's alright ;):

var stringOfHtml = "<p></p><script>alert('fail');</scr" + "ipt><span></span>";
var wrappedString = '<div>' + stringOfHtml + '</div>';
var noScript = wrappedString.replace(/script/g, "THISISNOTASCRIPTREALLY");
var html = $(noScript);
html.find('THISISNOTASCRIPTREALLY').remove();

alert(html.html().replace(/THISISNOTASCRIPTREALLY/g, 'script'));

JS Fiddle Workaround

JS Fiddle Example With Script Text

like image 114
Richard Dalton Avatar answered Oct 08 '22 17:10

Richard Dalton


Old question, but if you are still looking for the easiest way to remove scripts from a string try

$.parseHTML(string);  

$.parseHTML() automatically removes script tags from strings.

Edit:

This works because keepScripts has false value as default. (Reference: $.parseHtml)

$.parseHTML(data, context, keepScripts)
like image 43
Faraz Kelhini Avatar answered Oct 08 '22 16:10

Faraz Kelhini