Home » Html » How to replace window.open(…) with a POST

How to replace window.open(…) with a POST

Posted by: admin November 29, 2017 Leave a comment


I currently have some code that runs a window.open(urlWithGetParams) line. As far as I’m aware, this is going to force me to use a GET request. I would like to do this with a POST request. Is there a workaround for this?

I’m not married to window.open(), either. I’m open to just about any alternative that allows me to spawn a new window via a POST request instead of a GET.


In fact I made a small “library” just for this, open in POST a new window :

// Arguments :
//  verb : 'GET'|'POST'
//  target : an optional opening target (a name, or "_blank"), defaults to "_self"
open = function(verb, url, data, target) {
  var form = document.createElement("form");
  form.action = url;
  form.method = verb;
  form.target = target || "_self";
  if (data) {
    for (var key in data) {
      var input = document.createElement("textarea");
      input.name = key;
      input.value = typeof data[key] === "object" ? JSON.stringify(data[key]) : data[key];
  form.style.display = 'none';

Example :

open('POST', 'fileServer.jsp', {request: {key:"42", cols:[2, 3, 34]}});

To open in a new window, set the target parameter :

open('POST', someURL, someArgs, 'newwin');

or to ensure it’s a new window/tab each time :

open('POST', someURL, someArgs, '_blank');

Obviously you should give it a name other than open so that you don’t shadow the existing one. Ideally you should namespace it.


What I do is that I do a javascript AJAX post and then I take the content that I get back and place it into a new window.

Something like this (using jQuery, but you can use any AJAX implementation):

$.post(URL, DATA, function(d){
    var new_window = window.open();