首页 > IT职业 > IT职场 > jQuerify Bookmarklet
Reference: jQuerify
Bookmarklet,
Updated jQuery Bookmarklet and
Better, Stronger, Safer jQuerify Bookmarklet
What is jQuerify Bookmarklet?
A jQuerify Bookmarklet is a hyperlink that can be dragged into your bookmarks (or
favorites) toolbar. Then, when you’re on a page that doesn’t have jQuery, all you
have to do is click on the bookmarklet and you’ll be ready to play around with jQuery
on that page through the browser’s console.
This a simple jQuerify from the first article:
The source code:
Note: %20 here is the encode value of white space:
decodeURIComponent("%20") === " "; // true
Actually, when you click on the hyperlink or bookmarklet, you actually execute a
piece of JavaScript code:
var s=document.createElement('script'); s.setAttribute('src', 'http://jquery.com/src/jquery-latest.js'); document.getElementsByTagName('body')[0].appendChild(s); alert('thank you for using jquery!');
The second article jQuerify Bookemarklet:
The source code:
(function() { var s = document.createElement('script'); s.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js'); if (typeof jQuery != 'undefined') { var msg = 'This page already using jQuery v' + jQuery.fn.jquery; } else { document.getElementsByTagName('head')[0].appendChild(s); var msg = 'This page is now jQuerified' } var el = document.createElement('div'); el.style.position = 'fixed'; el.style.height = '30'; el.style.width = '200'; el.style.margin = '0 auto 0 auto'; el.id = 'jq-kswedberg'; el.style.top = '0'; el.style.left = '40%'; el.style.padding = '5px 10px 5px 10px'; el.style.backgroundColor = '#f99'; el.innerHTML = msg; var b = document.getElementsByTagName('body')[0]; b.appendChild(el); window.setTimeout(function() { jQuery('#jq-kswedberg').fadeOut('slow', function() { jQuery(this).remove() }); }, 2500); })();
There script code works well, but i want to improve it.
Because after we have loaded the jQuery library, we can show the message with the
help of jQuery.
My modified version:
(function() { function showMessage(msg) { var msgNode = $("").html(msg).css({ "background-color": "#F99", "padding": 10, "position": "fixed", "top": 0 }).appendTo("body"); msgNode.css("left", ($("html, body").width() - msgNode.width()) / 2); window.setTimeout(function() { msgNode.fadeOut("slow", function() { $(this).remove(); }); }, 2000); } var script = document.createElement("script"); script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"); if (typeof (jQuery) != "undefined") { showMessage("This page already using jQuery v" + jQuery.fn.jquery); } else { document.getElementsByTagName("head")[0].appendChild(script); script.onload = function() { showMessage("This page is now jQuerified"); }; } })();
This is my verson of jQuerify:
").html(msg).css({"background-color": "#F99","padding": 10,"position": "fixed","top": 0}).appendTo("body");msgNode.css("left", ($("html, body").width() - msgNode.width()) / 2);window.setTimeout(function() {msgNode.fadeOut("slow", function() {$(this).remove();});}, 2000);}var script = document.createElement("script");script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js");if (typeof (jQuery) != "undefined") {showMessage("This page already using jQuery v" + jQuery.fn.jquery);} else {document.getElementsByTagName("head")[0].appendChild(script);script.onload = function() {showMessage("This page is now jQuerified");};}})();void(0);'> My version of jQuerify
Everything seems ok until i move to IE, the message of “This page is now jQuerified”
disappears.
The reason is that script tag doesn’t support onload event.
I have to set interval to check whether the jQuery has been loaded.
Final version:
(function() { function showMessage(msg) { var msgNode = $("").html(msg).css({ "background-color": "#F99", "padding": 10, "position": "fixed", "top": 0 }).appendTo("body"); msgNode.css("left", ($("html, body").width() - msgNode.width()) / 2); window.setTimeout(function() { msgNode.fadeOut("slow", function() { $(this).remove(); }); }, 2000); } var script = document.createElement("script"); script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"); if (typeof (jQuery) != "undefined") { showMessage("This page already using jQuery v" + jQuery.fn.jquery); } else { document.getElementsByTagName("head")[0].appendChild(script); var timer = window.setInterval(function() { if (typeof (jQuery) != "undefined") { showMessage("This page is now jQuerified"); window.clearInterval(timer); } }, 200); } })();
").html(msg).css({"background-color": "#F99","padding": 10,"position": "fixed","top": 0}).appendTo("body");msgNode.css("left", ($("html, body").width() - msgNode.width()) / 2);window.setTimeout(function() {msgNode.fadeOut("slow", function() {$(this).remove();});}, 2000);}var script = document.createElement("script");script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js");if (typeof (jQuery) != "undefined") {showMessage("This page already using jQuery v" + jQuery.fn.jquery);} else {document.getElementsByTagName("head")[0].appendChild(script);var timer = window.setInterval(function() {if (typeof (jQuery) != "undefined") {showMessage("This page is now jQuerified");window.clearInterval(timer);}}, 200);}})();void(0);'> My Final version of jQuerify (support IE and Firefox)
How to use it?
Just drag the link to your bookmark or favorites list. When you want to add jQuery support to a page, click the bookmarklet.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/887/viewspace-2806195/ ,如需转载,请注明出处,否则将追究法律责任。