Why is this bookmarklet JS code not working when setting an onclick handler?
I have pretty typical bookmarklet code that works great for me in all browsers. However, when I take this code and put it in the onClick handler of the HTML element, it doesn't work in IE (6, 7 or 8).
This is the code:
javascript: (
function(){
function l(i,u){
var d=document;
var s;
try{
s=d.standardCreateElement('script');
}catch(e){}
if(typeof(s)!='object')
s=d.createElement('script');
try{
s.type='text/javascript';
s.src='http://{Domain}/bk/' + u;
s.id='s_' + i;
d.getElementsByTagName('head')[0].appendChild(s);
}catch(e){
}
}
AppD = '{Domain}';
l('b', 'bk.js');
}
)();
Compressed as a bookmarklet that looks like this:
javascript:function(){function l(i,u){var d=document;var s;try{s=d.standardCreateElement('script');}catch(e){} if(typeof(s)!='object') s=d.createElement('script'); try{s.type='text/javascript';s.src='http://{Domain}/bk/' + u;s.id='s_' + i;d.getElementsByTagName('head')[0].appendChild(s);}catch(e){}}AppD = '{Domain}';l('b', 'bk.js');})();
And it works great. I took out the javascript: prefix and put it on the onClick element:
<img onclick="function(){function l(i,u){var d=document;var s;try{s=d.standardCreateElement('script');}catch(e){} if(typeof(s)!='object') s=d.createElement('script'); try{s.type='text/javascript';s.src='http://{Domain}/bk/' + u;s.id='s_' + i;d.getElementsByTagName('head')[0].appendChild(s);}catch(e){}}AppD = '{Domain}';l('b', 'bk.js');})();" />
And this works well, except in IE, the code inside bk.js (the script that is being injected) complains that the AppD variable is not defined ...
Any ideas why this is happening?
Are there any restrictions on the code that can be put into an onClick handler?
Thanks! Daniel
a source to share
Solved by adding window.AppD before the variable declaration.
Solution provided by Andrew Noyce in another question:
Are there any restrictions on what can be done in the inline onclick handler?
a source to share