8

When we do a window.open(), is there an option to specify method = POST? Since by default, it is GET?

What I want is this. The parent window has some form parameters (many in number) and they should be sent to the server on window.open(). It is not a good idea to append all of them in the GET url using query string.

kapa
78.9k21 gold badges167 silver badges179 bronze badges
asked May 23, 2011 at 16:39

1 Answer 1

13

You could use window.open() to open an empty window, with a name. Then you could use a <form> with a "target" attribute referring to that new window's name, and post it.

edit OK here's the idea. You have a form on the page, and it can be hidden:

<form id='theForm' method='post' action='/your/action' target='TheNewWindow'>
 <input type='hidden' name='param_1' value='whatever'>
</form>

Then you get the results into your window like this:

window.open('about:blank', 'TheNewWindow');
document.getElementById('theForm').submit();

Make sure that the window name you use is a valid identifier (like a JavaScript variable name), or IE will get upset.

Here is a jsfiddle.

answered May 23, 2011 at 16:41
Sign up to request clarification or add additional context in comments.

5 Comments

@Kaushik yes I'm working on a small test now ... I want to make sure I get the details right :-)
Thank you. This will not work for me because this will always open a new window. But I forgot to mention that only on click of a particular button I want the new window, else, same window. But thanks for your effort, I will accept the answer
In that case, you can just clear out the "target" attribute of the <form> tag when when you don't need a new window (or, alternatively, set the "target" when you do want a new window).
Checked your fiddle on Chrome, Chrome version 39 ,it gets blocked by popup blocker!
@Mann odd. It still works in Firefox (33). Chrome's not showing any sort of obvious error.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.