来源于:http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code

http://stackoverflow.com/documentation/javascript/642/debugging

本身主要使用chrome浏览器,

所以调试主要是使用F12,然后在js文件里面增加“debugger;”

There is a debugger keyword in JavaScript to debug the JavaScript code. Put debugger; snippet in your JavaScript code. It will automatically start debugging the JavaScript code at that point.

For example:

Suppose this is your test.js file

function func(){
//Some stuff
debugger; //Debugging is automatically started from here
//Some stuff
}
func();
When the browser runs the web page in developer option with enabled debugger, then it automatically starts debugging from the debugger; point.
There should be opened the developer window the browser.

Breakpoints pause your program once execution reaches a certain point. You can then step through the program line by line, observing its execution and inspecting the contents of your variables.

There are three ways of creating breakpoints.

  1. From code, using the debugger; statement.
  2. From the browser, using the Developer Tools.
  3. From an Integrated Development Environment (IDE).

Debugger Statement

You can place a debugger; statement anywhere in your JavaScript code. Once the JS interpreter reaches that line, it will stop the script execution, allowing you to inspect variables and step through your code.

Developer Tools

The second option is to add a breakpoint directly into the code from the browser's Developer Tools.

Opening the Developer Tools

Chrome or Firefox

  1. Press F12 to open Developer Tools
  2. Switch to the Sources tab (Chrome) or Debugger tab (Firefox)
  3. Press Ctrl+P and type the name of your JavaScript file
  4. Press Enter to open it.

Internet Explorer or Edge

  1. Press F12 to open Developer Tools
  2. Switch to the Debugger tab.
  3. Use the folder icon near the upper-left corner of the window to open a file-selection pane; you can find your JavaScript file there.

Safari

  1. Press Command+Option+C to open Developer Tools
  2. Switch to the Resources tab
  3. Open the "Scripts" folder in the left-side panel
  4. Select your JavaScript file.

Adding a breakpoint from the Developer Tools

Once you have your JavaScript file open in Developer Tools, you can click a line number to place a breakpoint. The next time your program runs, it will pause there.

Note about Minified Sources: If your source is minified, you can Pretty Print it. In Chrome, this is done by clicking on the {} button in the bottom right corner of the source code viewer.

IDEs

Visual Studio Code (VSC)

VSC has built-in support for debugging JavaScript (see https://code.visualstudio.com/docs/editor/debugging).

  1. Click the Debug button on the left or Ctrl+Shift+D
  2. If not already done, create a launch configuration file (launch.json) by pressing the gear icon.
  3. Run the code from VSC by pressing the green play button or hit F5

Adding a breakpoint in VSC

Click next to the line number in your JavaScript source file to add a breakpoint (will be marked red). To delete the breakpoint, click the red circle again.

TIP: You can also utilise the conditional breakpoints in browser's dev tools. These help in skipping unnecessary breaks in execution. Example Scenario: You want to examine a variable in a loop exactly at 5th iteration.

如何调试js文件的更多相关文章

  1. Jmeter_前端RSA加密下的登陆模拟_引用js文件实现

    版权声明:本文为博主原创文章,未经博主允许不得转载. 在一次项目实战中,前端登录使用了RSA加密,使用LoadRunner压测的第一步,就是模拟用户登录,可惜loadRunner11并不能录制前端的加 ...

  2. Jmeter_前端RSA加密下的登陆模拟_引用js文件实现(转)

    在一次项目实战中,前端登录使用了RSA加密,使用LoadRunner压测的第一步,就是模拟用户登录,可惜loadRunner11并不能录制前端的加密过程,并且安装的LR是基于C语言版,网络上关于RSA ...

  3. ASP.NET MVC bootstrap 3 ie 8兼容问题及错误解决(取消IE禁用IE脚本调试定位js文件错误)

    因要做一个B/S架构的项目,使用MVC框架技术,本人不擅长页面设计美工,只好用bootstrap框架,在chrome内核系列的浏览器和IE 11中显示都没有问题,但是在 IE 8下显示却不正常,表格无 ...

  4. win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件

    win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件 我杀过 w3wp.exe和asp.net_state的进程,重启 iis admin的服务,都还是不行. 只是把 ...

  5. 如何调试异步加载的js文件(浏览器调试动态加载js)

    描述 1:jQuery->var obj= new $.js_Obj():等异步加载js文件,执行方法. obj.method(): 2:页面估计不变,通过声明不同的js文件,进行页面内容的转换 ...

  6. 手机端调试console.log,直接引入一个js文件

    http://files.cnblogs.com/files/lwwen/mConsole.js 这是我写的一个原生js文件 直接引入即可,可以把html上面的需要打印的东西打印出来 <!DOC ...

  7. chrome浏览器调试js,结果Sources里面找不到js文件解决办法

        页面出现问题,就debug,这是前端开发工程师最常见的做法,但是有时候,我们打开开发者工具,在sources查找js文件,却发现怎么也找不到,无法设置断点.但是文件在network选项卡里确实 ...

  8. 在电脑上用chrome浏览器调试android手机里的网页代码时,无法看到本地加载的js文件

    在需要调试的js文件最顶部加上代码就可以看到了: console.log('haha'); debugger;

  9. 动态调试JS脚本文件:(JS源映射 - sourceURL)与 debugger

    我们在进行js调试时经常会对js进行调试,chrome 对js提示对支持非常友好,只需要F12就可以打开chrome的调试器 在sources里面就是页面请求后加载的一些资源文件,我们可以找到我们的j ...

随机推荐

  1. 使用Openswan接入Windows Azure Site to Site VPN

    Winodows Azure的Site to Site VPN支持主流的防火墙和路由器等接入设备.具体型号和系列请参考下表: VENDOR DEVICE FAMILY MINIMUM OS VERSI ...

  2. Java入门(二)——果然断更的都是要受惩罚的。。。

    断更了一个多月,阅读量立马从100+跌落至10-,虽说不是很看重这个,毕竟只是当这个是自己的学习笔记,但有人看,有人评论,有人认同和批评的感觉还是很巴适的,尤其以前有过却又被剥夺的,惨兮兮的. 好好写 ...

  3. TeamTalk源码分析之服务端描述

    TTServer(TeamTalk服务器端)主要包含了以下几种服务器: LoginServer (C++): 登录服务器,分配一个负载小的MsgServer给客户端使用 MsgServer (C++) ...

  4. 【温故而知新-Javascript】使用 Ajax

    Ajax 是现代Web 应用程序开发的一项关键工具.它让你能向服务器异步发送和接收数据,然后用 Javascript 解析. Ajax 是 Asynchronous JavaScript and XM ...

  5. 第8章 用户模式下的线程同步(2)_临界区(CRITICAL_SECTION)

    8.4 关键段(临界区)——内部也是使用Interlocked函数来实现的! 8.4.1 关键段的细节 (1)CRITICAL_SECTION的使用方法 ①CRITICAL_SECTION cs;   ...

  6. 关于button和form提交的一些问题

    1.form 中type 为submit 和button的区别 submit是button的一个特例,也是button的一种,它把提交这个动作自动集成了. 如果表单在点击提交按钮后需要用JS进行处理( ...

  7. TP框架实现分页

    TP框架自带分页的实现方法,所以使用这个分页方案,不用再重新造轮子 1,先看效果图 2,源码 /** * TODO 基础分页的相同代码封装,使前台的代码更少 * @param $m 模型,引用传递 * ...

  8. php正则表达式治疗结巴

    用正则表达式去解决结巴这个问题可以通过下面进行解决: 解决思路是: 先找到重复的不部分 用str_replace($source,$replace,$str);来进行代理 下面分两种情况,最后将这两种 ...

  9. 布局 - panel

    panel一般作为其他组件的容器使用 很多组件都继承自panel 对于面板中的内容,支持异步从后台加载,当然,作为纯粹的面板,一般不会用到这个,但他的子类对于这个功能还是蛮实用的 <%@ tag ...

  10. DEDECMS之五 单页

    在网站开发中经常碰到关于我们.联系方式等简单的页面,那么在DEDECMS中如何实现? 一.效果 以上左侧导航的链接都是单页,右边为内容部分 二.单页的实现 创建频道封来实现 1.常规选项 2.高级选项 ...