2011-09-19 16:42
采纳率: 100%
浏览 462

在 JavaScript 控制台中包含 jQuery

Is there an easy way to include jQuery in the Chrome JavaScript console for sites that do not use it? For example, on a website I would like to get the number of rows in a table. I know this is really easy with jQuery.


The site does not use jQuery. Can I add it in from the command line?


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

18条回答 默认 最新

  • perhaps? 2011-09-19 16:44

    Run this in your browser's JavaScript console, then jQuery should be available...

    var jq = document.createElement('script');
    jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
    // ... give time for script to load, then type (or see below for non wait option)

    NOTE: if the site has scripts that conflict with jQuery (other libs, etc.) you could still run into problems.


    Making the best better, creating a Bookmark makes it really convenient, let's do it, and a little feedback is great too:

    1. Right click the Bookmarks Bar, and click Add Page
    2. Name it as you like, e.g. Inject jQuery, and use the following line for URL:

    javascript:(function(e,s){e.src=s;e.onload=function(){jQuery.noConflict();console.log('jQuery injected')};document.head.appendChild(e);})(document.createElement('script'),'//code.jquery.com/jquery-latest.min.js')

    Below is the formatted code:

    javascript: (function(e, s) {
        e.src = s;
        e.onload = function() {
            console.log('jQuery injected');
    })(document.createElement('script'), '//code.jquery.com/jquery-latest.min.js')

    Here the official jQuery CDN URL is used, feel free to use your own CDN/version.

    点赞 打赏 评论
  • 北城已荒凉 2011-09-19 16:45

    Run this in your console

    var script = document.createElement('script');script.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js";document.getElementsByTagName('head')[0].appendChild(script);

    It creates new script tag, fills it with jQuery and appends to head

    点赞 打赏 评论
  • Lotus@ 2011-09-19 16:50

    It's pretty easy to do this manually, as the other answers explain. But there's also the jQuerify plug-in.

    点赞 打赏 评论
  • from.. 2011-09-19 16:57

    Use the jQueryify booklet:


    Instead of copy pasting the code in the other answers, this'll make it a clickable bookmark.

    点赞 打赏 评论
  • 七度&光 2012-01-22 06:20

    If you're looking to do this for a userscript, I wrote this: http://userscripts.org/scripts/show/123588

    It'll let you include jQuery, plus UI and any plugins you'd like. I'm using it on a site that has 1.5.1 and no UI; this script gives me 1.7.1 instead, plus UI, and no conflicts in Chrome or FF. I've not tested Opera myself, but others have told me it's worked there for them as well, so this ought to be pretty well a complete cross-browser userscript solution, if that's what you need to do this for.

    点赞 打赏 评论
  • I'm a rebel.

    Solution: don't use jQuery. jQuery is a library to abstract the DOM inconcistencies across the browsers. Since you're in your own console, you don't need this kind of abstraction.

    For your example:


    ($$ is an alias to document.querySelectorAll in the console.)

    For any other example: I'm sure I can find anything. Especially if you're using a modern browser (Chrome, FF, Safari, Opera).

    Besides, knowing how the DOM works wouldn't hurt anyone, it would only increase your level of jQuery (yes, learning more about javascript makes you better at jQuery).

    点赞 打赏 评论
  • 7*4 2012-11-18 01:36

    The top answer, by jondavidjohn is good but I'd like to tweak it to address a couple of points:

    • Various browsers issue a warning when loading a script from http to a page on https.
    • Just changing jquery.com's protocol to https results in a warning if you try it straight from the browser's URL bar: This is probably not the site you are looking for!
    • I like to use Google's CDN when I'm using the console to experiment with Google sites such as Gmail.

    My only issue is that I have to include a version number where in the console I really always want the latest.

    var jq = document.createElement('script');
    jq.src = "//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
    点赞 打赏 评论
  • 乱世@小熊 2012-11-22 11:34

    Copy http://code.jquery.com/jquery-latest.min.js file content and paste it into console. Works perfect.

    点赞 打赏 评论
  • 斗士狗 2013-05-23 12:02

    Adding to @jondavidjohn's answer, we can also set it as a bookmark with URL as the javascript code.

    Name: Include Jquery


    javascript:var jq = document.createElement('script');jq.src = "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";document.getElementsByTagName('head')[0].appendChild(jq); setTimeout(function() {jQuery.noConflict(); console.log('jQuery loaded'); }, 1000);void(0);

    and then add it to the toolbar of Chrome or Firefox so that instead of pasting the script again and again, we can just click on the bookmarklet.

    Screenshot of bookmark

    点赞 打赏 评论
  • 笑故挽风 2013-05-24 12:31

    I just made a jQuery 3.2.1 bookmarklet with error-handling (only load if not already loaded, detect version if already loaded, error message if error while loading). Tested in Chrome 27. There is no reason to use the "old" jQuery 1.9.1 on Chrome browser since jQuery 2.0 is API-compatible with 1.9.

    Just run the following in Chrome's developer console or drag & drop it in your bookmark bar:

    javascript:((function(){if(typeof(jQuery)=="undefined"){window.jQuery="loading";var a=document.createElement("script");a.type="text/javascript";a.src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js";a.onload=function(){console.log("jQuery "+jQuery.fn.jquery+" loaded successfully.")};a.onerror=function(){delete jQuery;alert("Error while loading jQuery!")};document.getElementsByTagName("head")[0].appendChild(a)}else{if(typeof(jQuery)=="function"){alert("jQuery ("+jQuery.fn.jquery+") is already loaded!")}else{alert("jQuery is already loading...")}}})())

    Readable source-code is available here

    点赞 打赏 评论
  • 狐狸.fox 2014-01-18 06:25

    If you want to use jQuery frequently from the console you can easily write a userscript. First, install Tampermonkey if you are on Chrome and Greasemonkey if you are on Firefox. Write a simple userscript with a use function like this:

    var scripts = [];
    function use(libname) {
        var src;
        if (scripts.indexOf(libname) == -1) {
            switch (libname.toLowerCase()) {
                case "jquery":
                    src = "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js";
                case "angularjs":
                    src = "//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js";
        } else {
            console.log("Library already in use.");
        if (src) {
            var script = document.createElement("script");
            script.src = src;
        } else {
            console.log("Invalid Library.");
    点赞 打赏 评论
  • lrony* 2014-10-16 21:21

    FWIW, Firebug embeds the include special command, and jquery is aliased by default: https://getfirebug.com/wiki/index.php/Include

    So in your case, you just have to type :



    点赞 打赏 评论
  • Didn"t forge 2015-09-29 11:28

    Here is alternative code:

    javascript:(function() {var url = '//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; var n=document.createElement('script');n.setAttribute('language','JavaScript');n.setAttribute('src',url+'?rand='+new Date().getTime());document.body.appendChild(n);})();

    which can be pasted either directly in Console or create a new Bookmark page (in Chrome right-click on the Bookmark Bar, Add Page...) and paste this code as URL.

    To test if that worked, see below.


    Uncaught TypeError: $ is not a function(…)


    点赞 打赏 评论
  • 撒拉嘿哟木头 2015-12-11 15:15

    One of the shortest ways would be just copy pasting the code below to the console.

    var jquery = document.createElement('script'); 
    jquery.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
    点赞 打赏 评论
  • 7*4 2016-08-09 09:40

    Per this answer:

    fetch('https://code.jquery.com/jquery-latest.min.js').then(r => r.text()).then(r => eval(r))

    For some reason I have to execute it twice to get the new '$' (which I have to do with the other methods as well), but it works.

    This is the equivalent if your browser isn't so modern:

    fetch('http://code.jquery.com/jquery-latest.min.js').then(function(r){return r.text()}).then(function(r){eval(r)})
    点赞 打赏 评论
  • ℙℕℤℝ 2016-10-21 06:36

    this answer based on @genesis answer, at first I tried the bookmark version @jondavidjohn, and it is not working, so I change it to this (add it to your bookmark):

    javascript:(function(){var s = document.createElement('script');s.src = "//code.jquery.com/jquery-2.2.4.min.js";document.getElementsByTagName('head')[0].appendChild(s);console.log('jquery loaded')}());

    words of caution, is not tested in chrome but work in firefox, and not tested in conflict environment.

    点赞 打赏 评论
  • local-host 2017-01-18 02:59

    intuitive one-liner

    document.write(unescape('%3Cscript src="https://code.jquery.com/jquery-3.1.1.min.js"%3E%3C/script%3E’))

    You can change the src address.
    I referred to ReferenceError: Can't find variable: jQuery

    点赞 打赏 评论
  • 程序go 2018-09-14 11:19

    Turnkey solution :

    Put your code in yourCode_here function. And prevent HTML without HEAD tag.

    (function(head) {
      var jq = document.createElement('script');
      jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js";
      ((head && head[0]) || document.firstChild).appendChild(jq);
    function jQueryReady() {
      if (window.jQuery) {
      } else {
        setTimeout(jQueryReady, 100);
    function yourCode_here($) {
      $("body").html("<h1>Hello world !</h1>");

    点赞 打赏 评论

相关推荐 更多相似问题