热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何调试js文件

来源于:http:stackoverflow.comquestions988363how-can-i-debug-my-javascript-codehttp:sta

来源于: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 filefunction func(){//Some stuffdebugger; //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.



推荐阅读
  • 本文介绍了如何在使用emacs时去掉ubuntu的alt键默认功能,并提供了相应的操作步骤和注意事项。 ... [详细]
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • 无处不在,详解iOS集成第三方登录(SSO授权登录<无需密码>)
    1.前言 不多说,第三登录无处不在!必备技能,今天以新浪微博为例。这是上次写的iOS第三方社交分享:http:www.cnblogs.comqingchep3727559.html ... [详细]
  • vue使用
    关键词: ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • 本文介绍了OpenStack的逻辑概念以及其构成简介,包括了软件开源项目、基础设施资源管理平台、三大核心组件等内容。同时还介绍了Horizon(UI模块)等相关信息。 ... [详细]
  • 本文介绍了响应式页面的概念和实现方式,包括针对不同终端制作特定页面和制作一个页面适应不同终端的显示。分析了两种实现方式的优缺点,提出了选择方案的建议。同时,对于响应式页面的需求和背景进行了讨论,解释了为什么需要响应式页面。 ... [详细]
  • 本文介绍了网页播放视频的三种实现方式,分别是使用html5的video标签、使用flash来播放以及使用object标签。其中,推荐使用html5的video标签来简单播放视频,但有些老的浏览器不支持html5。另外,还可以使用flash来播放视频,需要使用object标签。 ... [详细]
  • 本文整理了Java中org.gwtbootstrap3.client.ui.Icon.addDomHandler()方法的一些代码示例,展示了Icon.ad ... [详细]
  • 本文整理了Java中org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc.getTypeInfo()方法的一些代码示例,展 ... [详细]
author-avatar
步履乘风
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有