Adding favicon to javascript Bookmarklet (uses window.open) Adding favicon to javascript Bookmarklet (uses window.open) javascript javascript

Adding favicon to javascript Bookmarklet (uses window.open)


Some of the things that I've tried that might possibly get you going a bit more:

Append a new link element to the current document:

javascript: var newLink = document.createElement('link');newLink.setAttribute('rel','icon');newLink.setAttribute('type','image/png');newLink.setAttribute('href','http://www.tapper-ware.net/devel/js/JS.Bookmarklets/icons/next.png');document.querySelector('head').appendChild(newLink);void(0);

Note that I was using the querySelector due to IE testing (though works in modern browsers as well). With Chrome and FF, I kept getting invalid character when trying to create the element, so I had to do piecewise attribute setting.

Tried using base64 encoded image string using the "data:image/png;base64,iVBORw0KGgoAAAA..." URI schema, but that didn't help anything due to the fact that I still had to set it to the current HTML text (which I could do, but ran into the same problem as you above of no bookmarklet).

Maybe this can't be done due to cross site scripting concerns? Not sure... Either way, really curious to see what you come up with (if you manage to come up with anything).


I tried and retried, and my first conclusion was: "It can't be done (at least not in FF4 on Ubuntu 11.04)". You need (I guess) a simple solution for your site visitors (drag&drop, add bookmark with 1 click ...).

I have found a workaround, it does it's job, but it is a little buggy (maybe someone can help fix it).
PROS:

  • add a icon to the bookmarklet
  • it uses windows.open
  • doesn't leave empty pages behind

CONS:

  • it reloads the current page (instead of leave a page behind)
  • Can't make Firefox POP-ul blocker allow "javascript:" generated HTML page to load POP-ups, so you need to hit allow every time

This is the code:

<a href="javascript:'<!DOCTYPE html><html><head><link rel="icon" type="image/png" href="http://www.tapper-ware.net/devel/js/JS.Bookmarklets/icons/next.png" /></head><body onLoad="window.open(\'http://example.com',\'test\',\'status=0,toolbar=0,location=0,menubar=0,resizable=false,scrollbars=false,height=379,width=379\');setTimeout(\'history.back(-1);\',100);"></body></html>';">Bookmarklet</a>

This is a link that you put on your page, the user needs to drag&drop this link to the bookmark bar (you can use something like Add Bookmark Script for adding it as a bookmark with 1 click), The bookmark has no icon until the user click's it at least once.

So how it supose to work:
1. redirect the user to the generated HTML page from the bookmarklet (that makes the ICON posible)
2. onLoad open the window you need using "windows.open"
3. redirect the page back using "history.back(-1)"

In theory everithing happens so fast, that the user does't see the new page, just that the current page is reloading, and a new windows appear.

The problem:
1. I use setTimeout for history.back beacause window.open is blocked by Firefox, so I need to click allow every single time (if somebody can fix this ... we have a chance of using this, develop it further :) )

I know THIS is not a reliable solution, but this is the only solution I've got so far.

Hope this helps a little. :)


"I don't want this opening in a new tab with a blank HTML file that then launches a popup.. Workaround..?"

If what you after really is the visual effect, you can try launch the blank HTML in hidden iframe, then launch the javascript.

Hope that helps