简体   繁体   English

找到用于在浏览器中调试 javascript 的侦听器的最快方法是什么?

[英]What is the fastest way to find a listener for debugging javascript in a browser?

Background:背景:

I often find myself in the position of debugging a piece of Java script on a web page in an unfamiliar codebase, and often one that has seen many developers and coding approaches.我经常发现自己在一个陌生的代码库中调试网页上的一段 Java 脚本,而且通常是一个见过许多开发人员和编码方法的代码库。 Sometimes I do not even what technologies might be in use, eg.有时我什至不知道可能正在使用什么技术,例如。 angular etc.角度等

The first time I need to address the Java script is when a specific behaviour is unexpected (ie. it has gone wrong.)我第一次需要处理 Java 脚本是在特定行为出乎意料(即出错)时。

Question: What tool provides the fastest route to identifying the entry point of the code that is causing the problem?问题:什么工具提供了最快的途径来识别导致问题的代码的入口点?

Example: I have an html element on a page lets say a button.示例:我在页面上有一个 html 元素,可以说是一个按钮。 When that button is clicked I expect to see an http request at the server.单击该按钮时,我希望在服务器上看到一个 http 请求。 There are many ways the element can be associated with its Java script listener.元素可以通过多种方式与其 Java 脚本侦听器相关联。 eg JQuery, thrid party plugins such as knockout etc, in house scripts, and so on.例如 JQuery、第三方插件(如 knockout 等)、内部脚本等。

Using developer tools I can start debugging this in the browser but only if I already know the entry point to put a breakpoint on.使用开发人员工具,我可以在浏览器中开始调试它,但前提是我已经知道要放置断点的入口点。

Is there a faster method to find the entry point than doing regular expressions searches on the pages code based on intuition and guess work to find what might be attached to that particular element?有没有比基于直觉和猜测工作在页面代码上进行正则表达式搜索以找到可能附加到该特定元素的内容更快的方法来找到入口点?

For me, the best starting point is in Chrome developer tools.对我来说,最好的起点是 Chrome 开发者工具。 You can:你可以:

  1. Choose an element in the elements tab在元素选项卡中选择一个元素
  2. On the right-hand side of the elements tree, click the "Event Listeners" tab.在元素树的右侧,单击“事件侦听器”选项卡。
  3. Find the event you want to debug (like click )找到您要调试的事件(如click
  4. Click the hyperlink to bring up the code for event listeners, and set breakpoints.单击超链接以调出事件侦听器的代码,并设置断点。 Sometimes you have to click the "format code" button (looks like { } ) to get the code on multiple lines so that the breakpoint is manageable.有时您必须单击“格式化代码”按钮(看起来像{ } )才能在多行上获取代码,以便断点易于管理。
  5. Do the click, and you'll hit your breakpoint, allowing you to step through the code, add watch variables, etc.单击一下,您将到达断点,从而允许您单步执行代码、添加观察变量等。

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

相关问题 从Web浏览器获取JavaScript服务器时间的最快方法是什么? - what is the fastest way to get server time in JavaScript from a web browser? 在javascript中查找字符串中存在哪些子字符串的最快方法是什么? - What is the fastest way to find which of the substrings is present in a string in javascript? 在浏览器中显示图像的最快方法是什么? - What's the fastest way to display images in browser? 将二进制数据传递到浏览器的最快方法是什么? - What is the fastest way to deliver binary data to browser? 使用javascript查找元素位置的最快方法? - fastest way to find element position using javascript? 在JS中找到路线的最快方法是什么? - What's the fastest way to find a route in JS? 有没有一种方法可以在调试时在浏览器中编辑JavaScript - Is there a way to edit a javascript in browser while debugging 在 JavaScript 中比较字符串的最快方法是什么? - What is the fastest way to compare strings in JavaScript? 在javascript中生成随机整数的最快方法是什么? - What is the fastest way to generate a random integer in javascript? 在 JavaScript 中平方一个数字的最快方法是什么? - What's the fastest way to square a number in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM