views:

189

answers:

2

Can a bookmarklet be used to overlay an image on a web page? Not as a pop-up, but as a image positioned by CSS and with a high z-index to display on top of other elements. And without a mask i.e., Shadowbox or similar jQuery effect. Just an image from a URL and positioned in the bottom left hand corner of the browser window.

This is what I have so far, but it may be the wrong direction to be going:

javascript:(function(){document.write("body {background-image:url(http://mydomain.com/image.png); position: absolute; left:50px; top:300px; z-index:9999;}");})()

I have a JS function that works as a bookmarklet to change the case of text on the page, and now I'd like to be able to show an image when the bookmarklet is used.

+1  A: 

i don't think document.write would be appropriate here; why not create a new img element and append it to the body?

var img = document.createElement('img');
img.src = 'urlhere';
// TODO: set position and whatnot
body.appendChild(img);
lincolnk
+1  A: 

Setting a background image for the body won't help if you want the image to overlay the page. Also, document.write can't be used directly to add CSS styles.

What you probably want to do is add a new img element to the page, and give it absolute or fixed positioning, along with a high z-index.

Here's an example that adds the SO logo to the corner of the window, using fixed positioning:

javascript:(function(){document.body.innerHTML += '<img src="http://sstatic.net/so/img/logo.png" style="position: fixed; left: 10px; bottom: 10px; z-index: 1000">';})();
Tobias Cohen
Thanks! Didn't realize I had the background image call in there. That works great for the SO logo and for the image I want to use. But... sometimes in IE with a page scrollable content, the image ends up at the bottom of the page out of view. Don't know how that would be fixed. Doesn't seem to be consistent, either.
songdogtech
Well, IE6 doesn't support `position:fixed`, so that could be your problem. There is a workaround for it, but it's really only useful when building a site from scratch, since it can break a lot of other CSS. Writing cross-browser bookmarklets is a difficult problem.
Tobias Cohen
It was IE8, but it must be a site-specific thing with complex CSS, and this will be used specifically on a simpler site. So it doesn't really matter. Thanks again.
songdogtech