Is it possible to change javascript variable values while debugging in Google Chrome?
I'm debugging a javascript app (using Chrome dev tools), and I would like to change some variable values while stepping through the code.
Is that possible at all?
I have tried and got something like:
> modeline
1
> modeline=0
0 <<< seems to work but...
> modeline
1 <<< ups!!
But I'm unable to find any documentation that states what can or can't be done...
Why is this answer still getting upvotes?
Per Mikaël Mayer's answer, this is no longer a problem, and my answer is obsolete (go()
now returns 30
after mucking with the console). This was fixed in July 2013, according to the bug report linked above in gabrielmaldi's comment. It alarms me that I'm still getting upvotes - makes me think the upvoter doesn't understand either the question or my answer.
I'll leave my original answer here for historical reasons, but go upvote Mikaël's answer instead.
The trick is that you can't change a local variable directly, but you can modify the properties of an object. You can also modify the value of a global variable:
var g_n = 0;
function go()
{
var n = 0;
var o = { n: 0 };
return g_n + n + o.n; // breakpoint here
}
console:
> g_n = 10
10
> g_n
10
> n = 10
10
> n
0
> o.n = 10
10
> o.n
10
Check the result of go()
after setting the breakpoint and running those calls in the console, and you'll find that the result is 20, rather than 0 (but sadly, not 30).
This is now possible in chrome 35 (today as of July 11, 2014). I don't know which version allowed it first though.
Just tested @gilly3 example on my machine and it works.
Open the console, in
Sources
and the tabSnippets
, add a new snippet, paste the following code into it:var g_n = 0; function go() { var n = 0; var o = { n: 0 }; return g_n + n + o.n; // breakpoint here }
Right-click the snippet name, click 'Run' (this does not fire the function though)
- Add the breakpoint at the return statement.
- In the console below, type
go()
- and change the variable values as demonstrated below
and the returned result g_n + n + o.n
is 30.
This is an acknowledged bug in the Chrome Dev Tools:
http://code.google.com/p/chromium/issues/detail?id=124206
It looks like not.
Put a breakpoint, when it stops switch to the console, try to set the variable. It does not error when you assign it a different value, but if you read it after the assignment, it's unmodified. :-/
Firebug seems to allow you to do that.
Yes! Finally! I just tried it with Chrome, Version 66.0.3359.170 (Official Build) (64-bit) on Mac.
You can change the values in the scopes as in the first picture, or with the console as in the second picture.
Actually there is a workaround. Copy the entire method, modify it's name, e.g. originalName() to originalName2() but modify the variable inside to take on whatever value you want, or pass it in as a parameter.
Then if you call this method directly from the console, it will have the same functionality but you will be able to modify the variable values.
If the method is called automatically then instead type into the console
originalName = null;
function originalName(original params..)
{
alert("modified internals");
add whatever original code you want
}
I'm able to modify a script variable value by assignment in the Console. Seems simplest.
I don't know why chrome team don't allow this silly feature ... but the only way I could change variable values with success is to modify the script directly in the chrome editor under "Sources" Tab (this changes the behavior of your script until you refresh the page), but that changes will lost when refresh, so be carefull.
I was having the same issue, went to the 'About Google Chrome'->Help and it said I needed to restart my browser to get the latest updates.
I did this, and suddenly, I can now change local variables. Simply click the variable you want to edit in the Scope Variables window, and type in your new value.
I did notice some oddities though, that I had to step over some unrelated var assignments before I could alter the text in the right hand window (Scope Variables).
'IT TIP' 카테고리의 다른 글
Difference between @Valid and @Validated in Spring (0) | 2020.10.21 |
---|---|
Smart Wrap in Vim (0) | 2020.10.21 |
How do I re-trigger a WebKit CSS animation via JavaScript? (0) | 2020.10.21 |
Position geom_text on dodged barplot (0) | 2020.10.21 |
Linking a static library to an iOS project in Xcode 4 (0) | 2020.10.21 |