4

Say I want to look at an object and so I log it to the console:

console.log(theNoticeObj);

Then using Chrome Dev Tools, I inspect it in the console and change its property theNoticeObj.bounceHeight to 10px

Now if I want to trigger theNoticeObj.bounce() on that object immediately to locate it, is there an easy way to do that from the console?

Thanks

EDIT:

Breakpoints where suggested below, but this freezes execution.

In face what I want is the command line API to work with javascript objects, not just DOM elements. If that were possible I'm sure I would be able to find it. I might go and see if there are any feature requests to that end for chrome. https://developers.google.com/chrome-developer-tools/docs/console#using_the_command_line_api

3 Answers 3

3

Try adding window.tno = theNoticeObject under the console.log statement. Reload the page and see if you can execute tno.bounce() from the console. If theNoticeObject is still in scope, this should work.

Sign up to request clarification or add additional context in comments.

1 Comment

That is what I have been doing previously - I was actually looking for a way to do it from the tool itself. But have an upvote for confirming one method.
1

You can navigate to the Sources tab and open your javascript file containing the piece of code you want to play with, in this case let us assume it is

console.log(theNoticeObj);

Once you find this line, you can set a breakpoint at this point and when your program execution comes to this line it will halt.

You can then use the Console tab to do operations on all the javascript objects in current local scope, window scope. You can simply call:

theNoticeObj.bounce();

It executes in the current context reflecting changes on the screen.

Hope this helps.

3 Comments

Have an upvote, but one problem is that this pauses execution - so bounce() will not actually execute until I press play. Which is good for most scenarios, but if I am trying to bounce() just to find the object on screen then this will not work. I guess I am after a "live" solution. I will ammend my question.
The execution of the javascript file in which you had added the breakpoint is paused. However the methods you run on console will execute and reflect its changes on the screen.
You're right! My apologies, the methods do execute on console and cause changes to the content on screen, but it wouldnt be visible until the execution is resumed. Very good question would be quite useful feature.
0

Now you can right click any object in the console and use "Store as global variable".

A new console line appears with the name of a new global variable holding reference to the selected object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.