How to Use the Post Method in Window.Open with Java Script

by L.P. Klages

The days of JavaScript being a toy language are long gone. Every serious web programmer has to know JavaScript and it's responsible for a lot of the processing and logic in modern web applications. One common task that JavaScript programmers face is loading a new page programatically, in other words, without any user interaction. This is possible using the built-in "" method, which works for most cases. But if you want to load a URL and pass POST variables to it at the same time, you'll need to do a little more work.

Copy and paste the following code to the top of the JavaScript file: window.openPost = function(url, variables) { var form = document.createElement("form"); form.setAttribute("method", "post"); form.setAttribute("action", url); for(variable in variables) { var hiddenField = document.createElement("input"); hiddenField.setAttribute("name", variable); hiddenField.setAttribute("value", variables[variable]); form.appendChild(hiddenField); } document.body.appendChild(form); form.submit(); }

Replace the "" method call with the "window.openPost()" method call you just created: window.openPost("your_post_file.html", ["post_variable_name": "variable_value", "post_variable_2": "another_variable_value" ]);

Open the page with the JavaScript in a web browser to see the code redirect to the specified page using the POST variables.


  • check Using this new method lets you post variables to a page just like the original method, but it doesn't allow you to specify the window options.

About the Author

L.P. Klages is an entrepreneur and software developer, concentrating on information theory, software user experience, and mathematical modeling. He has been writing about technology and the business of technology since 1999. His articles have appeared on many sites, including,, and eHow. Klages attended Jacksonville University in Jacksonville, Fla.

More Articles