I'm writing a chrome extension that fiddles around with the layout of a page. I want to use the latest version of jQuery to do this.
The page in question already includes version 1.4.4 of jQuery as part of one of their scripts.
If I include the newer version of jQuery, the page hangs. How can I include the newest version of jQuery so that it is only available to my content script, and doesn't affect scripts already on the page?
jQuery is a JavaScript library, so it operates on top of JavaScript. It cannot exist on its own, so you can't use one over the other. You can use just JavaScript or JavaScript and jQuery. jQuery was introduced to make development with JavaScript easier.
In jQuery's case, $ is just an alias for jQuery, so all the functionality is available without using $. Run $. noConflict() method to give control of the $ variable back to whichever library first implemented it. This helps us to make sure that jQuery doesn't conflict with the $ object of other libraries.
"content_scripts": [ { "matches": ["*://*.mozilla.org/*"], "js": ["borderify.js"] } ] Instructs the browser to load content scripts into web pages whose URL matches a given pattern. This key is an array.
A chrome extension is a program that is installed in the Chrome browser that enhances the functionality of the browser. You can build one easily using web technologies like HTML, CSS, and JavaScript.
If you inject your jquery as a content script it will be sandboxed, you won't be having any conflicts no matter what a parent page is using.
You can essentially namespace differing jQuery versions by using its No Conflict mode and executing your code in a closure. For instance:
<script src="jquery-1.6.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
jq162 = jQuery.noConflict(true);
(function ($) {
$(function () {
// Your document ready code here
});
})(jq162);
</script>
Please note this declaration makes jq162
available to the global scope so it can be reused elsewhere. If you'd prefer to scope the instance of jQuery locally, make it a var
.
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