- 1
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();
}
- 2
Replace the "window.open()" 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" ]);
- 3
Open the page with the JavaScript in a web browser to see the code redirect to the specified page using the POST variables.
5/5/11
How to Use the Post Method in Window.Open with Java Script
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 "window.open()" 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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment