How to suppress javascript errors in browsers?

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:

  1. Get HTML page from the URL to my server.
  2. Get all the CSS and image links from the page using htmlparser2(not javascript links)
  3. Parse CSS to check for any external links (i.e. image URLs, CSS import links etc.)
  4. Download these new resources and repeat step 3 & 4 until all resources are downloaded.
  5. Remove all the script tags from the HTML page (i do this using simple string manipulation).
  6. Add my own single script tag with a link to my javascript which is compiled using Google Closure (which adds a kind of sophisticated dynamic overlay using canvas) to the HTML.
  7. Open the URL of the downloaded HTML in the browser to serve the page from my server.
  8. The page opens in the browser but is served from my server, my javascript runs and the final result is displayed.

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
}