aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Prevent the first ready() handler from breaking the rest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Prevent the first ready() handler from breaking the rest" Watch "Prevent the first ready() handler from breaking the rest" New topic
Author

Prevent the first ready() handler from breaking the rest

Alec Swan
Ranch Hand

Joined: Feb 03, 2005
Posts: 39

Hello,

In our app we register multiple handlers on $(document).ready(). However, the first handler that throws an error also prevents the execution of subsequent handlers. It would be desirable to execute the rest of the handlers, e.g. in case when the failing handler was registered by some 3rd-party plugin we have no control over.

Has this been fixed in recent jQuery versions or is there a work-around for this issue?

Thanks,

Alec
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

That's not a jQuery bug to be fixed, but the way that JavaScript works. If you want to handle errors, a try-catch can be used.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alec Swan
Ranch Hand

Joined: Feb 03, 2005
Posts: 39

Except you can't put a try-catch around a third-party handler. It is not a jQuery bug, but jQuery could surely wrap handlers in try-catch effectively solving the problem.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Alec Swan wrote:Except you can't put a try-catch around a third-party handler. It is not a jQuery bug, but jQuery could surely wrap handlers in try-catch effectively solving the problem.


jQuery should not put try catches around code. The code should not throw in error in the first place. Fix the bug, do not mask it.

Eric

Alec Swan
Ranch Hand

Joined: Feb 03, 2005
Posts: 39

Sometimes your widget/plugin may be used on a page with a 3rd-party plugin that throws an error in the handler. The bug is in the 3rd-party code, not yours. So, even though your code is bug-free it doesn't work on that page making you look bad!
In general I believe event handlers should be able to work independently.
Why "jQuery should not put try catches around code" in all cases? It's impossible to require every plugin to a be a good citizen. That's why browsers catch JS errors and don't just crash instead.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

Alec Swan wrote:Sometimes your widget/plugin may be used on a page with a 3rd-party plugin that throws an error in the handler.

Eric's point is a good one. If jQuery were to just swallow exceptions, no one would ever get notification that there is a problem. So that's certainly not the answer.

The bug is in the 3rd-party code, not yours. So, even though your code is bug-free it doesn't work on that page making you look bad!

True. End users don't care where an error comes from. If the page fails, everyone looks bad.

In general I believe event handlers should be able to work independently.

They do. But any error in a script will cause the rest of the script to be skipped. Just like in any other language. If an unhandled exception occurs in Java, the stack is unwound to the point where the system catches it.

It's impossible to require every plugin to a be a good citizen.

Here I will disagree. Why should we tolerate buggy plugins? if a plugin is going to fail on startup, it's not one that I will use. Are there no alternatives to this buggy plugin that you could explore?

I suspect that what you really are asking for, is for jQuery to report any errors in ready handlers, but to continue to execute any other ready handlers. I'm not sure that I agree -- but it's certainly a more viable approach than simply ignoring errors.
Alec Swan
Ranch Hand

Joined: Feb 03, 2005
Posts: 39

If jQuery were to just swallow exceptions, no one would ever get notification that there is a problem. So that's certainly not the answer.

If an unhanded exception occurs in Java, the stack is unwound to point where the system catches it.

Swallowing an exception is one of the worst possible implementations. There are certainly ways to handle errors gracefully and keep handlers independent. Just like in Java where an exception on one thread does not kill all other threads.

Here I will disagree. Why should we tolerate buggy plugins?

Because we live in an imperfect world And buggy plugins make your plugin look bad as I explained in my previous posts.

Let's think of it in terms of the contract on the ready(function) method. What is the contract? Is the function guaranteed to execute? Hmm, only if there were no other handlers registered previously that threw an error. But how can I know if there are such other handlers registered previously? Wouldn't this depend on the order in which the web developer includes JavaScript files in their web page? Ah, so now the contract depends on the order of scripts in the page.

If jQuery handled errors and then surfaced them the contract would have been much simpler - the handler will always be executed.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

Alec Swan wrote:
Here I will disagree. Why should we tolerate buggy plugins?

Because we live in an imperfect world And buggy plugins make your plugin look bad as I explained in my previous posts.

To me that's a cop out. I won't use anything that's buggy by just waving it away and saying "that's the way it is". I'll either find a new plugin, or fix the buggy one to work the way I want.
Alec Swan
Ranch Hand

Joined: Feb 03, 2005
Posts: 39

To me that's a cop out. I won't use anything that's buggy by just waving it away and saying "that's the way it is". I'll either find a new plugin, or fix the buggy one to work the way I want.

I might not have been clear in explaining my scenario. Suppose that you develop a perfect plugin A and I develop a buggy plugin Z. Some poor web developer decides to use our plugins on a web page and happens to include Z before A. Then web developer starts testing plugin A, but it doesn't work! He files a bug and you spend half a day (if you are lucky) trying to figure out why your plugin did not work in his environment. You might even end up closing the bug as "not reproducible" pissing off the web developer and losing him as a customer.

All that could have been prevented by making jQuery a little bit smarter in the way it manages event handlers.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

I see your point, though still not agreeing that jQuery necessarily "needs fixing" because some people publish bad code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Prevent the first ready() handler from breaking the rest