问题
I'm trying to find a way to create a bookmarklet which will load (from an external file) a new layer/div with other html and css and overlay it on the existing page.
Does anyone have a bookmarklet example for this they could share please?
I can create the div with the new html content and CSS classes I am just not sure how to write the bookmarklet and javascript/jquery function to load new content on top of the existing page.
Thank you very much!
回答1:
Try this code, obviously it can be improved, but it works. You have to minifiy it and be sure to add "javascript:" before pasting it in your browser address bar.
javascript : (function (e, a, g, h, f, c, b, d) {
if (!(f = e.jQuery) || g > f.fn.jquery || h(f)) {
c = a.createElement("script");
c.type = "text/javascript";
c.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + g + "/jquery.min.js";
c.onload = c.onreadystatechange = function () {
if (!b && (!(d = this.readyState) || d == "loaded" || d == "complete")) {
h((f = e.jQuery).noConflict(1), b = 1);
f(c).remove()
}
};
a.documentElement.childNodes[0].appendChild(c)
}
})(window, document, "1.3.2", function ($, L) {
//your code here (a div with some content)
$("<div style='width:500px;height:400px; border:5px solid black; background-color:white; box-shadow:10px 10px 10px black; position:fixed; top: 150px; left:150px; z-index:99999'><h1 style='margin:150px'>hello world</h1></div>").appendTo("body");
});
EDIT:
here is the same code using an external document embedded in an iframe:
javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="http://ajax.googleapis.com/ajax/libs/jquery/"+g+"/jquery.min.js";c.onload=c.onreadystatechange=function(){if(!b&&(!(d=this.readyState)||d=="loaded"||d=="complete")){h((f=e.jQuery).noConflict(1),b=1);f(c).remove()}};a.documentElement.childNodes[0].appendChild(c)}})(window,document,"1.3.2",function($,L){$("<div style='width:500px;height:400px; border:5px solid black; background-color:white; box-shadow:10px 10px 10px black; position:fixed; top: 150px; left:150px; z-index:99999'><iframe style='width:100%; height:100%;' src='http://www.htmlcodetutorial.com/frames/hello.html'></iframe></div>").appendTo("body");});
来源:https://stackoverflow.com/questions/8326964/how-do-i-create-bookmarklet-to-overlay-html-div-layer-and-css-from-external-file