I made this bookmarklet:
javascript:(function(){var s=document.createElement('script');s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');document.getElementsByTagName('body')[0].appendChild(s);$('#hldIntMain').hide();$('#fadeBackground').hide();return false;})()
Formatted code:
// Add in jQuery
var s=document.createElement('script');
s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
document.getElementsByTagName('body')[0].appendChild(s);
// Make page viewable
$('#idIntMain').hide();
$('#idBackground').hide();
return false;
But whenever I run it, it loads a blank page after doing its work. What am I doing wrong?
For the blank page issue, you shouldn’t return
false, and since you are using an auto-invoking anonymous function, you can simply remove thereturnstatement.Functions by default, when there is no return statement is on the function body, they return the
undefinedvalue, and that will prevent navigating to the blank page.E.g.:
Will show a blank page containing the string representation of the returned value, “false” in this case.
The browser will not navigate.
After that I have a couple of comments:
The file will be loaded asynchronously, you can’t be 100% sure that jQuery will be loaded right after changing the
srcattribute of the element, you can use theloadevent of thescriptelement (readystatechangefor IE).I would also recommend you to append the
scriptelement to thehead.