Command for loading jQuery on Google Chrome inspector?

前端 未结 2 1286
無奈伤痛
無奈伤痛 2020-12-07 07:52

I remember seeing that there was a specific command you could put on Google Chrome\'s inspector console for it to load jQuery and allow you to execute jQuery commands.

相关标签:
2条回答
  • 2020-12-07 08:35

    You mean, a script to load jQuery in an arbitrary page? I have constructed the following cross-browser bookmarklet for this purpose:

    javascript:if(!window.jQuery||confirm('Overwrite\x20current\x20version?\x20v'+jQuery.fn.jquery))(function(d,s){s=d.createElement('script');s.src='https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.js';(d.head||d.documentElement).appendChild(s)})(document);
    

    It detects whether jQuery exists. If it does, a confirmation dialog appears, in which the current version is shown, so that you can decide whether it's OK to overwtite the existing jQuery object.

    Currently, jQuery 1.8 is loaded from a CDN over SSL.

    • If you want to load a different version, replace '1.8' with e.g. '1.7.1'.
    • If you want to load a compressed version, replace jquery.js with jquery.min.js.
    • If you don't mind loading jQuery over http:, Google's CDN can be replaced with:
      • http://code.jquery.com/jquery.js - Latest version
      • http://code.jquery.com/jquery-latest.js - Latest version
      • http://code.jquery.com/jquery-1.8.0.js - Version 1.8

    To save you time from editing, here's the same bookmarklet as the top of the answer, but getting the latest version (instead of a fixed one) from http://code.jquery.com/:

    javascript:if(!window.jQuery||confirm('Overwrite\x20current\x20version?\x20v'+jQuery.fn.jquery))(function(d,s){s=d.createElement('script');s.src='http://code.jquery.com/jquery.js';(d.head||d.documentElement).appendChild(s)})(document);
    

    Note: Having the latest version is nice, but don't be surprised when jQuery "behaves weird" (=updated).

    0 讨论(0)
  • 2020-12-07 08:46

    You can also create a chrome snippet which load jQuery on chrome inspector ( how create custom snippets )

    Snippet code:

    (function() {
      if (! window.jQuery ) {
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.async = true;
        s.src = '//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'; // you can change this url by latest jQuery version
        (document.getElementsByTagName('head')[0] ||
          document.getElementsByTagName('body')[0]).appendChild(s);
      }
    }());
    
    0 讨论(0)
提交回复
热议问题