Javascript bookmarklet go to URL and execute

Unfortunately your bookmarklet will not work. Change of window.location results loading of the new page - you cannot execute any Javascript on that page without having the document present first.

The desired action can be achieved by browser extensions, e.g. Greasemonkey script, which execute specified Javascript on the page upon page load.


I don't recommend browser extensions - they allow untrusted code to run in the context of trusted code. If you have a browser extension that loads a page and automatically enters your userID and password, what stops it from posting those credentials to a malicious site? Only you, and only if you've carefully read the source code.

To solve the problem above I've added bookmarklets to the favorites bar in my browser that require two clicks: 1. Click to navigate to the location 2. Click to fill the form

javascript: 
 var L='http://mysite.com/';
 if(location!=L)location=L;
 else{
  document.getElementById('username-id').value='User'; 
  document.getElementById('pwd-id').value = 'pass';
  document.close();
  doLogin(); 
 }

Good Luck!

Tags:

Javascript

Dom