views:

27

answers:

1

I realize this isn't terrific from a security perspective but humor me.

Is there a way to create a bookmarklet that submits a form, such as a login form. For example, this works, but only if there is a page loaded in the current browser window:

javascript:(function(){document.body.innerHTML += '<form id=f action=https://www.mysite.com/login method=post><input type=hidden name=un value=uname><input type=hidden name=pw value=pword>';document.getElementById("f").submit();})();

Is there some way to construct the form right in the JavaScript and submit that?

A: 

If you don't need the current page at all, you can completely replace it by having the bookmarklet expression return a string containing the complete new page:

javascript:'<body onload="document.forms[0].submit()"><form method="post" action="https://www.mysite.com/login"&gt;&lt;input type="hidden" name="un" value="uname"><input type="hidden" name="pw" value="pword"></form>'

then you don't have to worry about whether the previous page had a <body> or another element with id="f".

(And +1 Matti: innerHTML+= is always a mistake.)

bobince