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) orCmd+Option+J(Mac) - Mozilla Firefox:
Ctrl+Shift+K(Windows/Linux) orCmd+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
3. Inject jQuery from a CDN
If it is not present, load it from a CDN such as jQuery's official one:
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
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.
| Action | Command / Consideration |
| Open Console | Ctrl+Shift+J (Chrome), Ctrl+Shift+K (Firefox), etc. |
| Check if jQuery is loaded | window.jQuery ? 'Loaded' : 'Not Loaded' |
| Load jQuery | document.createElement('script') + appendChild |
| Check Version Compatibility | Ensure jQuery version is compatible with other scripts |
| Consider Security | use HTTPS, trusted CDNs |
| Example jQuery Use | $('selector').action() |
Related reading
- Infinite recursion in JavaScript quicksort?
- Initial state in React with Redux not working
- input typenumber/ is not showing a number keypad on iOS
- Insert HTML into view from AngularJS controller
- Install node in Dockerfile?
- Installing NPM on AWS EC2
- invalid ELF header when using the nodejs ref module on AWS Lambda
- Invariant Violation Application AwesomeProject has not been registered When building for iOS device with static jsbundle
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.