Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery in Chrome Console (8.0.552.237)

It appears that jQuery selectors are not functioning in the Chrome Console. Any type of selector returns "null". The scripts do properly run the javascript, however.

Has anyone else noticed this change or know of a fix.

Thanks.

like image 401
Tim Avatar asked Jan 25 '11 16:01

Tim


People also ask

Can Chrome console run jQuery?

appendChild(jqry); jQuery. noConflict(); It may take a bit of time for jQuery to load. But, once jQuery is loaded, you can run your custom jQuery functions in chrome console.

Can I write JavaScript in Chrome console?

You don't necessarily need to have an HTML page. Open Chrome, press Ctrl+Shift+j and it opens the JavaScript console where you can write and test your code.


1 Answers

I uncovered the cause of this in my own question.

The console injects its own function (just a shorthand) for document.getElementById(), aliased to $, which shadows jQuery's $. Easy way to check this: when you're at a breakpoint, and jQuery seems to be broken, compare the following in the console:

  • jQuery
  • $
  • window.$

The first and last will be jQuery proper, the local $ is something like:

function () {
    return document.getElementById.apply(document, arguments)
}

This is because code run from the console is wrapped in a with statement:

with (window ? window.console._commandLineApi : {}) {
with (window) {
    // the actual code you typed in here
}   
}

and window._commandLineApi.$ is the function that shadows jQuery.

stupid chrome


Found the bug in Chromium for this: http://code.google.com/p/chromium/issues/detail?id=70969

like image 51
Matt Ball Avatar answered Sep 27 '22 22:09

Matt Ball