views:

1140

answers:

1

Hi All,

I'm trying to use jQuery in a bookmarklet, which means I don't have control of the page into which the JavaScript will be inserted. From what I've read of noConflict, it requires that jQuery be inserted first. In the case of a bookmarklet, just the opposite is true.

So - does anyone have any suggestions on whether or not this is possible? I've found it works except on pages that are using prototype.js. On those pages, when I try to use the jQuery UI library, I get the error "this.cloneNode is not a function" in the bowels of jQuery, presumably because something in prototype is mucking with it.

Any help/advice would be appreciated.

A: 

noConflict has to be called after jQuery is included, and before some other library is included. So it's fine to include jQuery last, and then call noConflict -- it should then return the $ variable to whatever it was before jQuery was included.

It's sort of pain. Your bookmarklet needs to:

if(!jQuery) {
    include_jQuery();
    var $ = jQuery.noConflict();
} else {
    var $ = jQuery;
}
// do things with $

So as to avoid wrecking the page if it already had jQuery.

David
That's what I'm doing. In fact, I'm not even assigning or using $, I'm doing something like var myCustomVar = jQuery.noConflict(); and using that to prevent stomping on the rest of the page. My issue is that jQuery throws an error on cloneNode, which I think is being affected by prototype.
tlianza