Tags
Asked 2 years ago
9 Jun 2021
Views 372
Harley

Harley posted

Window.open and pass parameters by post method

Window.open and pass parameters by post method


<form method="post">
url:<input type="text" name="url"/>
window type:<input type="text" name="window_type"/>
window width :<input type="text" name="window_width"/>
window height :<input type="text" name="window_height"/>
</form>


i want to open window based on the form selected value , its just example form i given above
it can be exteneded as per need
eclipse-learner

eclipse-learner
answered Apr 27 '23 00:00

You can use the window.open() method to open a new window and pass parameters using the HTTP POST method by creating a form and submitting it through JavaScript.

Here is an example:



function openWindowWithPost(url, params) {
  var newWindow = window.open(url, "_blank");
  
  if (newWindow) {
    var form = newWindow.document.createElement("form");
    form.method = "POST";
    form.action = url;
    
    for (var key in params) {
      if (params.hasOwnProperty(key)) {
        var input = newWindow.document.createElement("input");
        input.type = "hidden";
        input.name = key;
        input.value = params[key];
        form.appendChild(input);
      }
    }
    
    newWindow.document.body.appendChild(form);
    form.submit();
  } else {
    alert("Pop-up blocked!");
  }
}

This function takes two parameters: url, which is the URL of the page to open, and params, which is an object containing the parameters to pass using the HTTP POST method.

The function first calls window.open() to open the new window. If the window is successfully opened, it creates a new form element with the HTTP POST method and action set to the specified URL. It then creates hidden input elements for each parameter in the params object and appends them to the form. Finally, it appends the form to the new window's document body and submits it to the server using the form.submit() method.

You can call the open WindowWithPost() function to open the new window and pass parameters using the HTTP POST method, like this:



var params = { name: "John", email: "john@example.com" };
openWindowWithPost("https://www.example.com/form.php", params);

This will open a new window with the URL "https://www.example.com/form.php" and pass the parameters { name: "John", email: "john@example.com" } using the HTTP POST method.
Post Answer