Bookmarklet Continues to Move to New Page

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?

+4
source share
1 answer

For a problem with a blank page, you should not return false , and since you are using an anonymous function with an automatic call, you can simply delete the return .

By default, when there is no return statement in the function body, they return undefined , and this will prevent navigation to a blank page.

eg:.

 javascript:(function () { return false; })(); 

Shows a blank page containing a string representation of the return value, "false" in this case.

 javascript:(function () {})(); 

The browser will not move.

After that I have a few comments:

The file will be loaded asynchronously, you cannot be 100% sure that jQuery will be loaded immediately after changing the element src attribute, you can use the load event of the script element ( readystatechange for IE).

I would also recommend adding a script element to head .

+5
source

Source: https://habr.com/ru/post/1303375/


All Articles