I have a tricky little problem in my hands that is threatening to put my work behind my atleast a week. I am trying to scrap a webpage from a URL and show that scraped webpage in my browser except for the javascript. my scraping happens in nodejs My programs flow is:
htmlparser2(not javascript links)Google Closure (which adds a kind of sophisticated dynamic overlay using canvas) to the HTML.But i ran into a problem in the 8th step.
I have removed all the script tags from the HTML page but there are certain pages which make javascript function calls from the the HTML itself using attributes like onload, onclick, etc.
For ex:
<body onload="xxx()">....</body>
Such pages raise an Uncaught ReferenceError: xxx is not defined in my browser.
Some browsers like Google Chrome log this error in the console but don't stop the execution, so my javascript runs without any issue.
But certain browsers like Firefox, Opera and IE (i am sure there would be more) stop the execution and enter the debugging mode and my javascript never runs.
I thought about removing all such attributes from my HTML on my server but then i found a list of all such attributes and decided against it for performance reasons since its a long list (i am still open to it if i can find an efficient way to do this).
I am looking for a way to handle all javascript errors that may come up in my HTML because of undefined references and then suppress them.
I can capture the errors using:
window.onerror = function(msg, url, line, col, error) {
alert(msg);
}
But can i do something to not break the execution flow when an Uncaught ReferenceError error occurs?
Basically, is there a way to catch and handle ReferenceError in javascript?
Thanx in advance!!
Try Catch is your friend
try {
// code
}
catch (e) {
// handle the exception or ignore
}