JavaScript
jQuery
Web Development
Coding
Browser Console

Load jQuery in the browser JavaScript console

Codemia Knowledge Hub ·

To use jQuery on a page that does not include it, open the browser's JavaScript console, create a <script> element whose src points to the jQuery CDN, and append it to the page's <head>. Once the script has loaded, $ and jQuery are available in the console.

1. Open the JavaScript console

  • Google Chrome: Ctrl+Shift+J (Windows/Linux) or Cmd+Option+J (Mac)
  • Mozilla Firefox: Ctrl+Shift+K (Windows/Linux) or Cmd+Option+K (Mac)
  • Safari: enable the Develop menu in preferences, then press Cmd+Option+C
  • Microsoft Edge: press F12, then switch to the "Console" tab

2. Check whether jQuery is already loaded

javascript
1if (window.jQuery) {  
2    console.log('jQuery is loaded');  
3} else {  
4    console.log('jQuery is not loaded');  
5}

3. Inject jQuery from a CDN

If it is not present, load it from a CDN such as jQuery's official one:

javascript
1var jqScript = document.createElement('script');
2jqScript.src = "https://code.jquery.com/jquery-4.1.0.min.js";
3
4// Optional: Report when it has loaded
5jqScript.onload = function () {
6    console.log('jQuery is now loaded');
7};
8jqScript.onerror = function () {
9    console.log('Failed to load jQuery');
10};
11
12document.getElementsByTagName('head')[0].appendChild(jqScript);

This creates a <script> element, sets its src to the jQuery library URL, and appends it to the document's <head>. Use a full https:// URL: a protocol-relative //code.jquery.com/... URL fails on pages opened from file://. If the page's own code expects jQuery 3, load jquery-3.8.0.min.js instead.

The script loads asynchronously, so window.jQuery is not defined on the next line. The onload handler runs once the download finishes, and from then on $ is available.

When the page blocks the script

If onerror fires and the console shows a Refused to load the script message mentioning Content-Security-Policy, the page's CSP does not allow scripts from code.jquery.com. This is the most common reason injection fails on large sites. Code you type into the console is not blocked by CSP, so a workaround is to open the jQuery .min.js file in another tab, copy its contents, and paste them straight into the console.

4. Use jQuery in the console

javascript
$('body').css('background-color', 'skyblue');

This changes the page's body background to sky blue.

What injecting jQuery is good for

  • DOM manipulation: quickly test and change elements.
  • CSS changes: try styles without editing CSS files.
  • Debugging: troubleshoot by calling jQuery functions.
  • Learning: practice jQuery commands on any page.

Compatibility, security and performance

  • Version compatibility: pick a jQuery version that does not conflict with other scripts on the page.
  • Security: external scripts can be exposed to man-in-the-middle attacks. Use HTTPS sources and trusted CDNs.
  • Performance: extra scripts affect page performance, so keep an eye on it if you use jQuery heavily this way.
ActionCommand / Consideration
Open ConsoleCtrl+Shift+J (Chrome), Ctrl+Shift+K (Firefox), etc.
Check if jQuery is loadedwindow.jQuery ? 'Loaded' : 'Not Loaded'
Load jQuerydocument.createElement('script') + appendChild
Check Version CompatibilityEnsure jQuery version is compatible with other scripts
Consider Securityuse HTTPS, trusted CDNs
Example jQuery Use$('selector').action()

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms