I'm viewing a webpage using Chrome. The original source does not include jQuery, but it'd be really helpful to use some jQuery functions to inspect the DOM. It would be a pain to alter the source to include the jQuery script, so is it possible to use Chrome Developer Tools to load jQuery to the DOM after the page loads? Alternately, is there a native JavaScript function that could perform the load?
I tried to edit the head tag to include the <script src="/path/to/jQuery.min.js"></script>
tag, but that doesn't actually load the jQuery.min.js file.
Unfortunately, Modernizr and other asset loaders are also not included with the source.
DevTools is a set of web developer tools that are built directly into the Google Chrome browser. DevTools allows you to view and change/manipulate the DOM, change a page's styles (CSS) in a preview environment, and work with JavaScript by allowing you to debug, view messages and run JavaScript code in the console.
To add a variable to the watch list use the add icon to the right of the section heading. This will open an inline input where you provide the variable name to watch. Once it is filled in press your Enter key to add it to the list. The watcher will show you the current value of the variable as it is added.
Click the Sources tab to open the Sources panel. Click the Page tab to show the page's resources. The Page pane opens.
You should be able to run this in your JS console.
var jq = document.createElement('script');
jq.src = "/path/to/jQuery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
jQuery.noConflict();
Alternatively you can copy the entire jQuery file into the console. Not the best option, but that would work too.
A third option is this plugin, jQueryify, I haven't used it myself, but looks promising.
require()
in the console.You can totally upgrade your testing workflow in Chrome developer console now that you can directly import npm modules into the console.
After installing requirify, you could easily add jquery to your console session by typing this into the developer console:
var $ = require('jquery');
This will fetch jQuery from npm, in turn making it available for you in the console. Now you can use jQuery in your console like you would on any webpage:
$('div').each(function(index){ //... });
Obviously this would work for your situation, but would also work in many other situations as well. A great tool to have in your toolbox!
See require()
being used in the console below:
Awesome!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With