如何调试js文件
来源于: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.
- From code, using the
debugger;
statement. - From the browser, using the Developer Tools.
- 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
- Press F12 to open Developer Tools
- Switch to the Sources tab (Chrome) or Debugger tab (Firefox)
- Press Ctrl+P and type the name of your JavaScript file
- Press Enter to open it.
Internet Explorer or Edge
- Press F12 to open Developer Tools
- Switch to the Debugger tab.
- 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
- Press Command+Option+C to open Developer Tools
- Switch to the Resources tab
- Open the "Scripts" folder in the left-side panel
- 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).
- Click the Debug button on the left or Ctrl+Shift+D
- If not already done, create a launch configuration file (
launch.json
) by pressing the gear icon. - 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文件的更多相关文章
- Jmeter_前端RSA加密下的登陆模拟_引用js文件实现
版权声明:本文为博主原创文章,未经博主允许不得转载. 在一次项目实战中,前端登录使用了RSA加密,使用LoadRunner压测的第一步,就是模拟用户登录,可惜loadRunner11并不能录制前端的加 ...
- Jmeter_前端RSA加密下的登陆模拟_引用js文件实现(转)
在一次项目实战中,前端登录使用了RSA加密,使用LoadRunner压测的第一步,就是模拟用户登录,可惜loadRunner11并不能录制前端的加密过程,并且安装的LR是基于C语言版,网络上关于RSA ...
- ASP.NET MVC bootstrap 3 ie 8兼容问题及错误解决(取消IE禁用IE脚本调试定位js文件错误)
因要做一个B/S架构的项目,使用MVC框架技术,本人不擅长页面设计美工,只好用bootstrap框架,在chrome内核系列的浏览器和IE 11中显示都没有问题,但是在 IE 8下显示却不正常,表格无 ...
- win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件
win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件 我杀过 w3wp.exe和asp.net_state的进程,重启 iis admin的服务,都还是不行. 只是把 ...
- 如何调试异步加载的js文件(浏览器调试动态加载js)
描述 1:jQuery->var obj= new $.js_Obj():等异步加载js文件,执行方法. obj.method(): 2:页面估计不变,通过声明不同的js文件,进行页面内容的转换 ...
- 手机端调试console.log,直接引入一个js文件
http://files.cnblogs.com/files/lwwen/mConsole.js 这是我写的一个原生js文件 直接引入即可,可以把html上面的需要打印的东西打印出来 <!DOC ...
- chrome浏览器调试js,结果Sources里面找不到js文件解决办法
页面出现问题,就debug,这是前端开发工程师最常见的做法,但是有时候,我们打开开发者工具,在sources查找js文件,却发现怎么也找不到,无法设置断点.但是文件在network选项卡里确实 ...
- 在电脑上用chrome浏览器调试android手机里的网页代码时,无法看到本地加载的js文件
在需要调试的js文件最顶部加上代码就可以看到了: console.log('haha'); debugger;
- 动态调试JS脚本文件:(JS源映射 - sourceURL)与 debugger
我们在进行js调试时经常会对js进行调试,chrome 对js提示对支持非常友好,只需要F12就可以打开chrome的调试器 在sources里面就是页面请求后加载的一些资源文件,我们可以找到我们的j ...
随机推荐
- 【转载国外好文】代工开发一个iOS应用没有那么容易
导读:这是来自新加坡的 iOS 开发者 Kent Nguyen 发表在1月底的一篇博文.这篇吐槽文在 iOS 开发圈子里流传甚广,从原文150多个评论就可见一斑,现翻译如下. 让我们开门见山吧:做一个 ...
- 学习OpenStack之 (0):基础知识
vi 方向键出现字母问题解决方法 执行命令 sudo apt-get remove vim-common 执行命令 sudo apt-get install vim 鼠标被virtualbox捕获无法 ...
- 浅析Java中的访问权限控制
浅析Java中的访问权限控制 今天我们来一起了解一下Java语言中的访问权限控制.在讨论访问权限控制之前,先来讨论一下为何需要访问权限控制.考虑两个场景: 场景1:工程师A编写了一个类ClassA,但 ...
- 在一周内学会使用 AUTO CAD
学习目的: 1.使用AUTO CAD绘制电路板外框.元器件封装: 2.借助软件使用,对产品结构有更深入的体会. 学习过程: 1.由于本人急需在短时间内具备简单的二维绘图能力,故没有借鉴.对比网络上其他 ...
- Eclipse 各种小图标的含义
Eclipse的Package Explorer和Outline中用图标表示了很多内容,其实能看懂这些图标在编程中也是很有用的. 绿色的小圆表示公有类型:如果是实心的就是函数(),如果是圈就表示是变量 ...
- 《Python核心编程》部分代码习题实践(持续更新)
第三章 3-10 交换异常处理方式 代码: #makeTextFile.py #!/usr/bin/env python 'makeTextFile.py' import os ls = os.lin ...
- POJ 1556 The Doors【最短路+线段相交】
思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...
- NOIP2010乌龟棋[DP 多维状态]
题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...
- Maven学习(四)Maven 命令行选项
说明: 1.使用-选项时,和后面的参数之间可以不要空格.而使用--选项时,和后面的参数之 间必须有空格.如下面的例子: $ mvn help:describe -Dcmd=compiler:co ...
- React 学习笔记(一)
React + es6 一.createClass 与 component 的区别 The API (via 'extends React.Component') is similar to Reac ...