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 the return
statement.
Functions by default, when there is no return statement is on the function body, they return the undefined
value, and that will prevent navigating to the blank page.
E.g.:
javascript:(function () { return false; })();
Will show a blank page containing the string representation of the returned value, "false" in this case.
javascript:(function () {})();
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 src
attribute of the element, you can use the load
event of the script
element (readystatechange
for IE).
I would also recommend you to append the script
element to the head
.
精彩评论