Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all JavaScript from an HTML page

I've tried using the Sanitize gem to clean a string which contains the HTML of a website.

It only removed the <script> tags, not the JavaScript inside the script tags.

What can I use to remove the JavaScript from a page?

like image 547
user1049097 Avatar asked Nov 28 '11 05:11

user1049097


People also ask

How do I block JavaScript in HTML?

Press Control+Shift+P or Command+Shift+P (Mac) to open the Command Menu. Start typing javascript , select Disable JavaScript, and then press Enter to run the command. JavaScript is now disabled. The yellow warning icon next to Sources reminds you that JavaScript is disabled.

How do I remove content from HTML?

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


1 Answers

I am partial to the Loofah gem. Modified from an example in the docs:

1.9.3p0 :005 > Loofah.fragment("<span onclick='foo'>hello</span> <script>alert('OHAI')</script>").scrub!(:prune).to_s
 => "<span>hello</span> " 

You might be interested in the ActiveRecord extensions Loofah provides.

like image 124
the Tin Man Avatar answered Oct 18 '22 08:10

the Tin Man