Opening a new browser window with POST data

Every now and again I want to have a link that opens a new browser window, and passes some values in. It’s easy when you just need to pass in GET data:

But it becomes harder when you want to pass in POST values.

Maybe not that hard…How about opening the new window, adding the POST variables to the form, and then posting it to the relevant domain. That would work :)

This code gives you a button which will open a new window and automatically log in to your WordPress blog.


        <script type='text/javascript'>
        function LoginToWordPressBlog()
        var sData;
        var sUserName = "YourUserName";
        var sPassword = "YourPassword";
        var sDomain = "";

        sData = "<form name='loginform' id='loginform' action='" + sDomain + "/wp-login.php' method='post'>";
        sData = sData + "<input type='text' name='log' id='user_login' class='input' value='" + sUserName + "' />";
        sData = sData + "<input type='password' name='pwd' id='user_pass' class='input' value='" + sPassword + "' />";
        sData = sData + "<input type='submit' name='wp-submit' id='wp-submit' value='Login »' />";
        sData = sData + "<input type='hidden' name='redirect_to' value='/wp-admin/' />";
        sData = sData + "<input type='hidden' id='wordpress_test_cookie' name='wordpress_test_cookie' value='WP Cookie check' />";
        sData = sData + "</form>";
        sData = sData + "<script type='text/javascript'>";
        sData = sData + "document.cookie='wordpress_test_cookie=home; expires=Fri, 11 Jul 2009 05:23:14 +0000; path=/';";
        sData = sData + "document.loginform.submit();</sc" + "ript>";"", "newwin");
        <form id="form1">
                 <input id="Button1" type="button" value="Login To Wordpress" onclick='LoginToWordPressBlog()' />

The method above is in javascript, so it should work for pretty much everybody. The steps are:

  1. Create a new window
  2. Use document.write to write the HTML POST data tags to the new window.
  3. Submit the new window.

The only tough part now is finding out which POST tags you need - and the best tool for that is Fiddler. It even shows you the cookie values that you need.

This post brought to you by - Over 10,000 Aged domains for sale!

Related posts:

  1. MySpace API
  2. Copying cookies across domains in ASP.Net
  3. MySpace API - Automate your Blog posts
  4. Hiding the Digg button from Diggers
  5. Easy Cross Domain Cookies (Sharing cookies between domains)