Can jQuery throw an error from my event handlers?

为君一笑 提交于 2020-01-04 09:37:06

问题


I have an error that occurs in a specific event handler that I setup using jQuery and the code just breaks without any errors being displayed. When I used to code similar things in YUI I would setup YAHOO.util.Event.throwErrors = true; in my dev. version and that was telling YUI's event handler to throw errors. I can't find anything similar, is it just me being blind and not seeing it ?

Thanks!

Here's an example...

<html>
<head>
<title>jQuery Events</title>
</head>
<script src="https://www.google.com/jsapi"></script>
<script>
google.load('jquery', '1.3.2');
</script>
<body>
<p>click me</p>
<script type="text/javascript">
    $(document).ready(function() {
        $("p").click(function() {
            console.log('How to display an error here similar to the native onclick ?');
            qaz;
            console.log('this line is not executed because of the error, but the error is not reported. Is there a way to have jQuery report those errors ?');
        });
    });
</script>
</body>
</html>

回答1:


I'm pretty certain that jQuery won't swallow errors for you, so you should definitely be seeing the error from your undefined qaz.

Running your updated page, I still see both the console.log and the error on the undefined qaz in Firebug. Opening up the normal browser error console, I also see the qaz is not defined (...temp.html - Line: 15) message there too. In IE, I of course get "console" is undefined, but that just shows that errors are getting through there as well (since there's no console). Assuming you're running that exact same piece of html/js, there's obviously some other variable in play that is causing issues for you.

What version of Firebug are you running? I had various random issues with debugging javascript & the like with version 1.4.x, but 1.5.0 seems much more consistent and stable.



来源:https://stackoverflow.com/questions/2158365/can-jquery-throw-an-error-from-my-event-handlers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!