简体   繁体   English

挂钩JavaScript中的所有函数调用?

[英]Hooking all function calls in JavaScript?

My intuitive response to this question would be ,"This is so stupid that I can't continue having this conversation." 我对这个问题的直觉回答是:“这太愚蠢了,我无法继续进行这次对话。” However: 然而:

Is there any way to hook all javascript function calls within my module? 有什么办法可以挂接模块中的所有javascript函数调用? I'd like to find a convenient way of showing "loading blah...", "done..." messages when performing AJAX calls without having to explicitly place a call to a notification method in every AJAX method. 我想找到一种方便的方法来执行AJAX调用时显示“ loading blah ...”,“ done ...”消息,而不必在每个AJAX方法中显式地调用通知方法。

Also, is there any convenient way to set up a global exception handler in javascript? 此外,是否有任何简便的方法可以在javascript中设置全局异常处理程序?

It's perfectly possible, of course, to eliminate the need for either of these things by performing correct exception handling in every applicable method, but it would be nice to have a global "Whoops!" 当然,完全有可能通过在每种适用的方法中执行正确的异常处理来消除对这两种东西的需要,但是最好有一个全局的“哇!” method to happily catch anything that managed to slip through due to programmer (ie, me) error. 高兴地捕获由于程序员(即,我)错误而无法通过的任何内容的方法。

Working in C#/ASP.NET if it matters or if there's a server-side gadget that could make this easier. 如果重要或使用服务器端小工具可以简化操作,请使用C#/ ASP.NET。

window.onerror will catch all of the errors. window.onerror将捕获所有错误。 That's how Firebug and others work. 这就是Firebug和其他人的工作方式。

As for "every ajax method" you should just have 1 reusable ajax method, and have it be in charge of updating your status message. 对于“每个ajax方法”,您应该只具有1个可重用的ajax方法,并负责更新您的状态消息。

If you only have ajax calls actually being made in one place, where the open and send commands are, then you can put in some logging, or notification there. 如果实际上只有一个地方(即open和send命令所在的地方)进行了ajax调用,那么您可以在其中进行一些日志记录或通知。

If you are using jquery, or some library that supports selectors, you can bind to the events of many objects. 如果使用的是jquery或某些支持选择器的库,则可以绑定到许多对象的事件。

The exception part was already answered. 异常部分已被回答。

You can look at all objects via function show_props(obj, obj_name) { var result = "" for (var i in obj) result = obj_name "." 您可以通过函数show_props(obj,obj_name){var result =“” for(var i in obj)result = obj_name“查看所有对象。 i " = " obj[i] "\\n" return result; i“ =” obj [i]“ \\ n”返回结果; } That is from http://www.memestreams.net/users/acidus/blogid10323750/ }那是从http://www.memestreams.net/users/acidus/blogid10323750/

I suggest that you use jQuery AOP plugin. 我建议您使用jQuery AOP插件。 I have used it for a large library. 我已将其用于大型图书馆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM