Question

I have a bookmarklet like this:

javascript:(function()%7Bvar%20x=document.createElement('SCRIPT');x.type='text/javascript';x.src='http://tmxhost.com/mail/clippad.js?'+(new%20Date().getTime()/100000);document.getElementsByTagName('head')%5B0%5D.appendChild(x);%7D)();

But when I tap it nothing happens until I tap it a second time, then it writes the DOM. Anyone know why?

Was it helpful?

Solution

The bookmarklet appends this script to page: http://tmxhost.com/mail/clippad.js

That script has two parts. 1.) Add jQuery to page. 2.) Use jQuery.

The problem is that there is no check that jQuery has loaded before the second part is run, so usually the second part will fail on the first try.

When you click the bookmarklet the 2nd time, jQuery has finished loading, so the second part runs as expected.

There are some good tools and tutorials for using jQuery in a bookmarklet: https://www.google.com/search?q=jquery+bookmarklet

Another point to consider is that using $(document).ready is generally pointless in a bookmarklet because a bookmarklet is almost always used after the document is ready.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top