Search This Blog

Sunday, September 7, 2014

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

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.
 

//Remove (script) tags from a string of html 
var page_content = $("html").clone().find("script,noscript,style").remove().end().html();  

// Remove SCRIPT - LINKS TAG From Ajax Response 

    

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.


No comments:

Post a Comment

Contact Form

Name

Email *

Message *