Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I debug javascript contained in $(document).ready()?

I'm trying to debug some js in the browser (Chrome specifically). How can I check what value is set for some_data and new_data?

I realized due to variable scope being confined to functions, some_data and new_data don't exist after the document ready() was executed.

$(document).ready(function(){
   var some_data = [4, 8, 15, 16, 23, 42];
       var new_data = some_data * 2;
});
like image 446
Don P Avatar asked Mar 21 '13 18:03

Don P


People also ask

Is there a way to debug JavaScript?

But fortunately, all modern browsers have a built-in JavaScript debugger. Built-in debuggers can be turned on and off, forcing errors to be reported to the user. With a debugger, you can also set breakpoints (places where code execution can be stopped), and examine variables while the code is executing.

What is $( document ready () method in jQuery?

jQuery ready() Method The ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs.

Which file is used to debug the JavaScript files?

But the one we're interested in right now is the Sources panel, which helps us in debugging JavaScript. You can open DevTools by pressing the F12 Key, or using a shortcut: either Control+Shift+I (Windows, Linux) or Command+Option+I (Mac). Click the Sources tab to navigate to the Sources panel.


3 Answers

Use developer tools. If you're using chrome, hit F12, go to sources, find the file your javascript is in, find your code, set a breakpoint (by clicking to the left of the line you want the breakpoint on), and hit F10 to execute line by line. You can just hover over the variable names and it'll give you the current values.

like image 170
Phillip Schmidt Avatar answered Oct 12 '22 23:10

Phillip Schmidt


my two cents ...

write the word debugger inside of the code... and will generate a breakpoints instantly, if you hit (ctrl + i) will show you the debugger controls...

best

like image 28
ncubica Avatar answered Oct 12 '22 22:10

ncubica


You can use developer tool bar in IE, Chrome. For firefox use Firebug, this is a Firefox add-on. For Safari use inspect element. All of most browser support below key:

F10- execute line by line(step over next function call)
F11- (step into next function call)
F8- Pause script execution.
like image 20
Amit Avatar answered Oct 12 '22 22:10

Amit