acdha (owner)

Forks

Revisions

gist: 89968 Download_button fork
public
Description:
A template for creating bookmarklets which load jQuery from the Google CDN
Public Clone URL: git://gist.github.com/89968.git
bookmarklet-template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
To avoid polluting the global namespace this uses an anonymous
function which is called with either the URL for an external
JavaScript file or a function. In either case jQuery will be loaded
from the Google CDN before your code is executed so it's free to
depend on jQuery without checking for it and can do things like
jQuery.getScript() to load other components (e.g. jQuery UI),
stylesheets, etc.
*/
(function (target, msg) {
    var loader = function() {
        // Avoid executing this function twice:
        if (arguments.callee._executed) return;
        arguments.callee._executed = true;
        
        if (typeof target === "function") {
            target();
        } else {
            jQuery.getScript(target);
        }
        
        // Now that we're done with the actual work, let's display a
        // confirmation message so the user knows it's ready:
        var el = document.createElement('div');
        el.style.position='absolute';
        el.style.height='30px';
        el.style.width='200px';
        el.style.margin='0 auto 0 auto';
        el.style.top='0';
        el.style.left='40%';
        el.style.padding='5px 10px 5px 10px';
        el.style.backgroundColor='#f99';
        el.innerHTML=msg;
        document.body.appendChild(el);
 
        window.setTimeout( function() { jQuery(el).fadeOut('slow', function() { jQuery(this).remove(); } ); }, 2500 );
    };
 
    if (typeof jQuery !== 'undefined') {
        // Nice - this page already uses jQuery!
        loader();
    } else {
        var s = document.createElement('script');
        s.type = "text/javascript";
        s.setAttribute('src', document.location.protocol + '//ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js');
 
        if (s.addEventListener) { // Mozilla / WebKit
            s.addEventListener("load", loader, false);
        } else if ("onreadystatechange" in s) { // IE
            s.onreadystatechange = function () {
                  if (this.readyState == 'complete' || this.readyState == 'loaded') { loader(); }
            };
        } else {
            // Chances are if your browser is this old jQuery won't even work but just in case:
            window.setTimeout(loader(), 2500);
        }
 
        document.getElementsByTagName('head')[0].appendChild(s);
    }
 
// Note the use of document.location.protocol to avoid http/https
// mismatches. Obviously this requires a server such as pantheon which
// can handle either protocol:
})(document.location.protocol + '//path.to.example.com/something.js', "Page Enhanced!");
 
// If you just need a little jQuery, pass a function instead of the URL to a .js file:
//})(function(){ jQuery('body').append('<p>Hello!</p>'); }, "Page vandalized!");