Mispy
Mispy

Reputation: 879

Expose variable to the devtools console without making it global on window

Is it possible to make a variable easily accessible by the debug console without actually making it global? For example, something like this in the code:

console.$ = jQuery

And then in the console, I'd like this to work:

$('#foo')

Upvotes: 2

Views: 373

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074445

Not while the page isn't paused at a breakpoint, no. But when the page is paused at a breakpoint, the console has access to all variables that are in-scope at the point where it's stopped.

Of course, your console.$ = jQuery example does make it possible to do

console.$("#foo")

...in the console.


Side note: console.$ = jQuery and $("#foo") aren't great examples, as jQuery is almost always global, and even if you don't have jQuery loaded on your page at all, Chrome provides its own $ function in the console, and I think at least one other browser does as well...

Upvotes: 4

Related Questions