Syndicate

Feed

Using Greasemonkey to load and use jQuery while being considerate to John's bandwidth

When I googled how to use jQuery in a Greasemonkey script — a script to run on a web page that already loads some other, conflicting, JavaScript framework, like Prototype — I found no script that worked. Not a fucking one. They all contained very wrong bits, and I wander why they're still published. They're still indexed so that people like me can experience more pain. Here's what I came up with after much labor. I believe it is fail-proof.

[]


// ==UserScript==
// @name name
// @namespace namespace
// @description Does jQuery stuff while playing nice with other JavaScript frameworks
// @author Caroline Schnapp
// @homepage http://11heavens.com/using-greasemonkey-to-load-and-use-jQuery
// @include *
// ==/UserScript==
 
// Add jQuery
var script = document.createElement('script');
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(script);
// When jQuery is loaded
script.addEventListener('load', function(){ 
  jQuery = unsafeWindow['jQuery'];
  jQuery.noConflict();
  /* You put your jQuery code here, which must use the jQuery namespace. See Note. */
}, false);
Note: Use the “jQuery” namespace, not the dollar sign “$”, in your code.

How it all works — me thinks

My code makes the following assumptions:

  • The greasemonkey script is run when the DOM is ready. Greasemonkey just does stuff to a document when it is ready.
  • My script adds a script tag to the head and waits till the jQuery library has loaded to do its thing to the body.
  • You can use addEventListener with the event 'load' on a script element, and your callback function will be executed when the .js file has finished loading.
  • The DOM is already ready at the time you're loading jQuery (see 1st point), so no need to use the jQuery(function {}); construct in your code later on.

jQuery is hosted on Google — I always forget that

You can find a link to google-hosted latest and best jQuery minified right here.
Last edited by Caroline Schnapp about 4 years ago.

Comments

your solution stands out

your solution stands out where no other worked sir.
i am using the $ variable just fine too :)

script.addEventListener('load', function(){ 
  $ = unsafeWindow['jQuery'];
  $.noConflict();

Special Content!

This is compared to anything else is extra. The other way is not a lot of people have a choice. But this one is special, as well as abnormal.
Honeymoon Destinations in india | Best Honeymoon Destinations in India

When I googled how to use

When I googled how to use jQuery in a Greasemonkey script — a script to run on a web page that already loads some other, conflicting, JavaScript framework, like Prototype — I found no script that worked. Not a fucking one. They all contained very http://e-papierossy.com.pl/en/e-papierosy/99-e-papierosy-gambit-1221.html wrong bits, and I wander why they're still published. They're still http://e-papierossy.com.pl/en/e-papierosy/80-e-papierosy-gambit-123445.html indexed http://e-papierossy.com.pl/en/e-papierosy/76-e-papierosy-gombit-423456.htmlso that people like me can experience more pain. Here's what I came up with after much labor. I believe it is fail-proof.

script.addEventListener('load

script.addEventListener('load', function(){
jQuery = unsafeWindow['jQuery'];
jQuery.noConflict();
(function($){
/* You put your jQuery code here, which must use the jQuery namespace. See Note. */
console.log($);

})(jQuery);
}, false);

So you can use your $ inside the anonymous function. :)

script.addEventListener('load

script.addEventListener('load', function(){
jQuery = unsafeWindow['jQuery'];
jQuery.noConflict();
(function($){
/* You put your jQuery code here, which must use the jQuery namespace. See Note. */
console.log($);

})(jQuery);
}, false);

So you can use your $ inside the anonymous function. :)

Hello every one

Nicely written information in this post, I prefer to read this kind of stuff. The quality of content is fine and the conclusion is fine. kings casino | my bet.com

my favorites programming language

Jquery is one of my favorites programming language. it is extremely fast and easy to learn:) Greasemonkey is an another great thing I use this a lot of.

http://topiarysg.com

I learn something new and

I learn something new and challenging on websites I stumbleupon on a daily basis. It's always exciting to read through content from other writers and use something from their web sites. Estate tax

i use that too

i use that too and i find that it is very easy to use and user friendly

something special

this is a special page to do jquery. pasir ris new ec ecopolitan ec corals at keppel bay

The Skywoods

This is a nice post in an interesting line of content.Thanks for sharing this article
by The Skywoods Condo
The Skywoods
The Skywoods
The Skywoods @ Dairy Farm
The Skywoods
The Skywoods @ Dairy Farm

Vous avez parlé tout le

Vous avez parlé tout le contenu est bon, peut être très utile. Je m'en souviendrai, merci pour le partage de l'information est constamment mis à jour, s'attendant plus posts.Thanks

Bishan Condo facilities

Bishan Condo facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Bishan. Bishan Condo

Web Designers

WordPress Website Pros are the UK's leading WordPress Website Design company specialising in stunning, CMS mobile responsive websites and SEO.

Useful Info

Well-written. Learned something new. Not just some random stuff but useful things that I need to know. Thanks!
Active Domain

Additional knowledge

For me, this is something essential that can add up to my knowledge when it comes to programming specifically scripts. Might not use it for now but I'm pretty sure this will come in handy someday. Kudos!

Wai Kay Photography

thanks to you

I've been stuck with these scripts for quite some time now and thanks to you, I am now on the next step of my task. Works like magic.

Active Venture

Well, well, well

Been searching for this. Good thing I visited this blog since I rarely do view blogs in my computer because I usually go to websites. Wow! This changed my perception when it comes to blogs. This is one of those things that I can say that there are some blogs which are even better than several websites. Knowledge acquired! You can count on me that I will tell this good news to my friends.

iHub Solutions Malaysia

Helps me

After spending a lot of time with jQuery and Greasemonkey, this is certainly something that can improve my experience of using these scripts. For me, these scripts are great but this article has made them even better since it has helped for these scripts to be easier to use.

reading intervention

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <css> <html> <javascript> <mysql> <php> <span> <a> <b> <i> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <sup> <sub> <dd> <del> <blockquote> <img> <q> <p> <div>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <javascript>, <mysql>, <php>, <rails>, <ruby>.

More information about formatting options

CAPTCHA
I have to wonder if you're a human spammer or a machine, or less likely someone who cares to leave his or her thoughts behind.