jQuery: remove (script) tags from a string of html

I'm in the middle of writing a bookmarklet to screen scrape web pages. My back-end piece is having trouble parsing pages with lots of script tags. Since I don't actually need the script tags, I thought I would remove them before it even gets to the back-end. Here is how to grab the current page HTML and strip some tags from it, without changing the DOM the user is actually viewing.

var page_content = $("html").clone().find("script,noscript,style").remove().end().html();

That's it! I then POST the html content to the back-end with an ajax call. The key bit is the clone(), which copies the entire page so that we can remove element from the copy and not without removing it from the UI the users if viewing.



I'm currently working at NerdWallet, a startup in San Francisco trying to bring clarity to all of life's financial decisions. We're hiring like crazy. Hit me up on Twitter, I would love to talk.

Follow @chase_seibert on Twitter