0

I have a script which I want to allow users to debug right in the browser. I can get line numbers for the breakpoints and I can get local variable values what I'm curious about is how do I pause the actual script execution at a specific line? I tried putting hidden sleep() calls along with the local vars dumping code, wonder if there's more elegant solution for the problem?

Just a clarification - the console isn't available to me. I can only use regular JS script features. I use chrome dev tools for years but never experienced the need for emulating debugger; instruction by JS code before.

user1514042
  • 1,899
  • 7
  • 31
  • 57
  • see here: http://stackoverflow.com/questions/66420/how-do-you-launch-the-javascript-debugger-in-google-chrome This is the command: debugger; – mihaisimi Jan 07 '15 at 15:27
  • or use `console.log`s everywhere :) – lante Jan 07 '15 at 15:27
  • if it's for educating purpose, maybe the best way is to inform your user to open the console instead of finding a may-not-exist tricky way. – vilicvane Jan 07 '15 at 15:53
  • not it's not. I thought of visualizing the interim execution state along with giving access to the actual var values. The first aspect is even more useful. – user1514042 Jan 07 '15 at 15:57

4 Answers4

2

Use the developer console of your web browser. For example in Firefox, hit CTRL+SHIFT+S, find the specific line, and click on it. It will stop the execution, when it reaches that line.

You can use the usual debugging steps too:

  • Step Over F10,
  • Step Into F11
  • Step Return SHIFT+F11

PS: I also recommend using the Firebug extension, I find it invaluable in my job. It is a developer toolbar with many neat features, and better performance (at least it seems that way for me).

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
0

There is no inline way to pause javascript however using the chrome debugger tools you can have it stop at a certain breakpoint.

In chrome: Ctrl+Shift+I > sources > script.js > click a line number > refresh page

Simon Staton
  • 4,345
  • 4
  • 27
  • 49
0

Use google chrome console ctrlshiftj go to Source tab search the js file and click on line number for put a breakpoint

ppascualv
  • 1,137
  • 7
  • 21
0

Use the debugger; statement:

The debugger statement invokes any available debugging functionality, such as setting a breakpoint. If no debugging functionality is available, this statement has no effect.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger

blgt
  • 8,135
  • 1
  • 25
  • 28
  • How do I get it working just in the browser without using a console? I'd thought it'll simply get ignored it console isn't active – user1514042 Jan 07 '15 at 15:42
  • It will, and you don't. Why would your users want to let you hijack their browser? – blgt Jan 07 '15 at 15:49
  • cos I love my users and try doing anything to make their lives easier:) – user1514042 Jan 07 '15 at 15:51
  • Well dev tools cannot be opened programmatically. With javascript being asynchronous, consider attaching your script to a callback of the *big red button* type. But then this becomes a usability question, not a programming one – blgt Jan 07 '15 at 15:57