Reputation: 6509
I'm trying to do a screen scrape using jquery in a PhoneGap application. The problem is that when I load the target HTML into the current dom so I can use selectors, it attempts to execute the javascript:
$(function() {
$.get('http://www.bloomberg.com/quote/csfb:ind', function(data) {
var elements = $("<div>").html(data)[0].getElementsByClassName("price")[0];
});
});
The get works fine and pulls in the html, but once it starts loading, it tries to execute the javascript.
Is there anyway to strip out all javascript tags?
Upvotes: 0
Views: 1237
Reputation: 35117
This seems like the most reliable way as any parsing you come up with on your own may not work in all cases.
Removing all script tags from html with JS Regular Expression
If I copy the stripScripts function from that answer:
function stripScripts(s) {
var div = document.createElement('div');
div.innerHTML = s;
var scripts = div.getElementsByTagName('script');
var i = scripts.length;
while (i--) {
scripts[i].parentNode.removeChild(scripts[i]);
}
return div.innerHTML;
}
$(function () {
$.get('http://www.bloomberg.com/quote/csfb:ind', function (data) {
var elements = $("<div>").html(stripScripts(data))[0].getElementsByClassName("price")[0];
});
});
Upvotes: 1