25

Our product inserts a script into client's websites, kind of like a live chat box.

Often, clients' websites have buggy javascript that also stops our code (the browser stops execution when errors are encountered). Is there any way to make our code still execute even though there are errors in the console about things like undefined methods or variables?

Thanks for your help.

10
  • 1
    Theoretically, not unless you run your script before anything else on the page (which is usually not possible, or even desirable). Commented May 31, 2012 at 0:25
  • 3
    Kinda like when the airplane engine fails isn't there a way to just make it keep going Commented May 31, 2012 at 0:25
  • 4
    I don't think you should babysit customers' sites. If their sites are buggy, it's their own problem. It'd also motivate them to actually fixing the code instead of adding workaround after workaround. Commented May 31, 2012 at 0:27
  • @AlfalfaStrange close, but this is less akin to a general failure, and more likened to an actual explosion. :-) +1 Commented May 31, 2012 at 0:27
  • 1
    @D3mon-1stVFW: Are you serious about that? An applet? We have 2012, not 2000. But yes, don't waste time on making your stuff work on broken sites. If they want it they should fix their stuff. Commented May 31, 2012 at 0:51

5 Answers 5

26

The short answer is that you really can't.

"Solution" #1: You could insist that YOUR 3rd party code run before anyone else's. In most cases, this isn't possible or even desirable.

"Solution" #2: You could insist that the 1st party engineers wrap all 3rd party code in try/catch blocks. But, this solution really doesn't buy you any guarantee, because very frequently 3rd party libraries attach additional <script> tags to the page - these would not fall under the "jurisdiction" of the try/catch scope enclosing the code which created this/these tag(s).

"Solution" #3: You could build YOUR app entirely within the scope of an <iframe>, thereby avoiding the issue entirely. Unfortunately, even if you're very smart, you'll quickly run into cross domain violations, 3rd party cookie restrictions, and the like. It's very probable that this will not work for you.

"Solution" #4: You could explain the issue to your client, and have them demand that the other 3rd party code run cleanly. I say this is a "solution" because, frankly, it's not a "solution" to your question if your question is how to avoid doing exactly this.

Unfortunately, option #4 is your best bet. It may help if you observe other 3rd party libraries "breaking" in the same fashion: you can tell your client "hey, it's not just me - X, Y, and Z are all also 'broken' because of <name of other 3rd party library>." It may cause them to put the heat on the offending code, which makes the web a happier place for all involved.

Sign up to request clarification or add additional context in comments.

2 Comments

So wrapping the functions in a 'closure' like $(function() { function myLibraryFunction() {...} } will not help and isolate the code execution like it isolates the function names and variables?
Uncaught errors thrown in one script (<script></script>) should only abort that script and not other scripts in the page. Even then, there could potentially be a recovery/retry...for instance if a setTimeout was called before the uncaught error occurred that allowed for checking state, restarting, retrying, etc. The original issue was probably related to their chat script being loaded by a "tag management" system (often single script) that was buggy itself or didn't do a good job of catching/handling errors for various third-party scripts the tag management was responsible for loading.
20

As others have said, continuing after an error might not be the best thing to do but you can try this:

function ignoreerror()
{
   return true
}
window.onerror=ignoreerror();

More details here

The onerror event fires whenever an JavaScript error occurs (depending on your browser configuration, you may see an error dialog pop up). The onerror event is attached to the window object, a rather unusual place to take refuge in, but for good reason. It is attached this way so it can monitor all JavaScript errors on a page, even those in the section of the page.

Opera has a page with more details

Browsers supporting window.onerror
Chrome 13+
Firefox 6.0+
Internet Explorer 5.5+
Opera 11.60+
Safari 5.1+

3 Comments

Note the MDN docs: "Note that some/many error events do not trigger window.onerror, you have to listen for them specifically." (developer.mozilla.org/en/DOM/window.onerror)
I have found that window.onerror still causes code to stop executing. All that return true seems to do is suppress the exception from appearing in the console. The following code (tested in Chrome 42 and IE 10) illustrates my point: window.onerror = function(){return true;}; var s = ""; s.push("H"); alert("test"); The error is suppressed but the alert code is never reached.
Using onerror return true; or addEventListener .preventDefault() just prevents logging to the console. An uncaught error always aborts the CURRENT SCRIPT. The key is to separate your code into a separate script BLOCK (e.g. <script></script>). The problem is likely that their code is loaded by a single "tag management" script that is buggy itself or doesn't isolate errors for loaded scripts....so when error occurs it isn't handled and so they don't get a chance to load.
0

You can't from your code - they need to use try/catch for questionable pieces of script.

Comments

0

You could have them insert an iframe into their page instead of you trying to inject code using a script tag like so: http://jsfiddle.net/EzMGD/ Notice how the script throws an error yet we can still see the content in the iframe. The iframe should help from using each others variables if applicable.

<script>
    MeaningOfLife();
</script>

<iframe src="http://bing.com"></iframe>​

Or inject the code so it's the very first or very last script.

1 Comment

The problem here is that you can easily and quickly run into 3rd party cookie restrictions and cross domain security policies... these make iframe development for anything smarter than a social badge an absolute pain.
0

Well, to me that work fine:

element = document.querySelector('.that-pretty-element');
if (element != null) {
    element.onclick = function () {
        alert(" I'm working beibi ;) ");
    }
}

querySelector() returns false, so, we can verify with if's

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.