My bookmarklet consist of a call to a ‘launcher’ script that is inserted into body. As it runs, it inserts in a similar way more scripts (jQuery, the actual application), and a CSS.
Bookmarklet
javascript:(function(){
var l = document.createElement('script');
l.setAttribute('type','text/javascript');
l.setAttribute('src','launcher.js');
document.body.appendChild(l);
})();
Launcher.js
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
s.setAttribute('src','my actual script');
document.body.appendChild(s);
// I repeat a block like this once per script/css.
The problem is that if the bookmarklet is clicked twice, all the scripts will be inserted again. How can I prevent this behavior?
You can set a property on the
windowobject which is oddly named, so it is unlikely to cause namespace collisions, on the first run only. On subsequent runs, the code will not execute if the property is present: