if you want to pass POST variables, you have to use a HTML Form:

<form action="http://localhost:8080/login" method="POST" target="_blank">
    <input type="text" name="cid" />
    <input type="password" name="pwd" />
    <input type="submit" value="open" />
</form>

or:

if you want to pass GET variables in an URL, write them without single-quotes:

http://yourdomain.com/login?cid=username&pwd=password

here's how to create the string above with javascrpt variables:

myu = document.getElementById('cid').value;
myp = document.getElementById('pwd').value;
window.open("http://localhost:8080/login?cid="+ myu +"&pwd="+ myp ,"MyTargetWindowName");

in the document with that url, you have to read the GET parameters. if it's in php, use:

$_GET['username']

be aware: to transmit passwords that way is a big security leak!

Answer from Roman Abt on Stack Overflow
Top answer
1 of 7
15

if you want to pass POST variables, you have to use a HTML Form:

<form action="http://localhost:8080/login" method="POST" target="_blank">
    <input type="text" name="cid" />
    <input type="password" name="pwd" />
    <input type="submit" value="open" />
</form>

or:

if you want to pass GET variables in an URL, write them without single-quotes:

http://yourdomain.com/login?cid=username&pwd=password

here's how to create the string above with javascrpt variables:

myu = document.getElementById('cid').value;
myp = document.getElementById('pwd').value;
window.open("http://localhost:8080/login?cid="+ myu +"&pwd="+ myp ,"MyTargetWindowName");

in the document with that url, you have to read the GET parameters. if it's in php, use:

$_GET['username']

be aware: to transmit passwords that way is a big security leak!

2 of 7
4

Please find this example code, You could use hidden form with POST to send data to that your URL like below:

function open_win()
{
    var ChatWindow_Height = 650;
    var ChatWindow_Width = 570;

    window.open("Live Chat", "chat", "height=" + ChatWindow_Height + ", width = " + ChatWindow_Width);

    //Hidden Form
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", "http://localhost:8080/login");
    form.setAttribute("target", "chat");

    //Hidden Field
    var hiddenField1 = document.createElement("input");
    var hiddenField2 = document.createElement("input");

    //Login ID
    hiddenField1.setAttribute("type", "hidden");
    hiddenField1.setAttribute("id", "login");
    hiddenField1.setAttribute("name", "login");
    hiddenField1.setAttribute("value", "PreethiJain005");

    //Password
    hiddenField2.setAttribute("type", "hidden");
    hiddenField2.setAttribute("id", "pass");
    hiddenField2.setAttribute("name", "pass");
    hiddenField2.setAttribute("value", "Pass@word$");

    form.appendChild(hiddenField1);
    form.appendChild(hiddenField2);

    document.body.appendChild(form);
    form.submit();

}
Discussions

javascript - Window.open and pass parameters by post method - Stack Overflow
Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... With window.open method I open new site with parameters, which I have to pass by post method.I've found solution, but unfortunately it doesn't work. This is my code: More on stackoverflow.com
๐ŸŒ stackoverflow.com
Javascript window.open confusing parameters? - Stack Overflow
Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... _blank - URL is loaded into a new window... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Opening a window using windows.open() in Javascript - Stack Overflow
This parameter is necessary because all window.open parameters are positional, that means that if you want to set specs you need to define URL and name. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Stack Overflow chat opening up ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
window.open - JavaScript window open passing dynamic URL parameters - Stack Overflow
The window opens fine, but the parameters passed to it are "karr" and "kval"; I want to pass the values of karr and kval. I can't figure out how to do that, and can't find any information in an online search. Can this be done? More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 16, 2022
Top answer
1 of 10
147

Instead of writing a form into the new window (which is tricky to get correct, with encoding of values in the HTML code), just open an empty window and post a form to it.

Example:

<form id="TheForm" method="post" action="test.asp" target="TheWindow">
<input type="hidden" name="something" value="something" />
<input type="hidden" name="more" value="something" />
<input type="hidden" name="other" value="something" />
</form>

<script type="text/javascript">
window.open('', 'TheWindow');
document.getElementById('TheForm').submit();
</script>

Edit:

To set the values in the form dynamically, you can do like this:

function openWindowWithPost(something, additional, misc) {
  var f = document.getElementById('TheForm');
  f.something.value = something;
  f.more.value = additional;
  f.other.value = misc;
  window.open('', 'TheWindow');
  f.submit();
}

To post the form you call the function with the values, like openWindowWithPost('a','b','c');.

Note: I varied the parameter names in relation to the form names to show that they don't have to be the same. Usually you would keep them similar to each other to make it simpler to track the values.

2 of 10
78

Since you wanted the whole form inside the javascript, instead of writing it in tags, you can do this:

let windowName = 'w_' + Date.now() + Math.floor(Math.random() * 100000).toString();
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "openData.do");

form.setAttribute("target", windowName);

var hiddenField = document.createElement("input"); 
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "message");
hiddenField.setAttribute("value", "val");
form.appendChild(hiddenField);
document.body.appendChild(form);

window.open('', windowName);

form.submit();
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74819985 โ€บ javascript-window-open-passing-dynamic-url-parameters
window.open - JavaScript window open passing dynamic URL parameters - Stack Overflow
December 16, 2022 - function switchyn(obj,id,karr,kval){ // ... Some stuff about obj and id .... var fullurl = 'arch-update.cfm?karr=' + karr + '&kval=' + kval; window.open(fullurl); }
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 43765595 โ€บ opening-new-window-with-parameter-in-javascript
jstl - Opening new window with parameter in javascript - Stack Overflow
I have below javascript function . I am trying to open a jsp in new window and pass some values from this function : function editStudent(fname){ alert("hi"); var editWindow = window.open("
Top answer
1 of 2
1

There are a number of issues here. First, of all, the values you use in your example (top=500,left=2000,height=600,width=400) are likely to put at least part of the window off screen. This will be automatically corrected by the browser. As MDN notes:

Requested position (top, left), and requested dimension (width, height) values in windowFeatures will be corrected if any of such requested value does not allow the entire browser popup to be rendered within the work area for applications of the user's operating system. In other words, no part of the new popup can be initially positioned offscreen.

Secondly, in the code where you are checking the position, you're actually reporting the main window's position, not the popup's. Instead of:

console.log("top=" + window.screenY + ", left=" + window.screenX);

That should be:

console.log("top=" + popwin.screenY + ", left=" + popwin.screenX);

This example correctly positions and reports the position of a popup window:

<!DOCTYPE html>
<head>
  <script type="text/javascript">
    function popup() {
      const popwin = window.open(
        "", 
        "", 
        "top=300,left=300,height=40,width=400,titlebar=no,popup=yes"
      );  
      popwin.document.write(`top=${popwin.screenY}, left=${popwin.screenX}`);
    }   
    popup();
  </script>
</head>
<body>
</body>
</html>

Lastly, concerning the address bar: it is in most modern browsers no longer possible to open a popup window without displaying the address bar, for security reasons. See more info in this answer.

In conclusion, there are better alternatives than using popups. They annoy users, and are routinely blocked by browsers. Depending on your use case, you could use something like a dialog element.

2 of 2
0

Try this code:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Popup Test</title>
    </head>
    <body>
        <center>
            <p>test page for popup window</p>
        </center>
        <br />
        <br />
        <center><button onClick="newwin()">Click to open window</button></center>
        <br />
        <br />
        <script type="text/javascript">
            var popwin = null;

            function newwin() {
                // Get the current window position
                var top = window.screenY + 100;
                var left = window.screenX + 100;

                // Open a new window at the calculated position
                popwin = window.open(
                    "",
                    "",
                    `top=${top},left=${left},height=600,width=400,titlebar=no,scrollbars=yes,location=no`
                );
                popwin.document.write("<p>XXXXX</p>");

                console.log("top=" + top + ", left=" + left);
            }
        </script>
    </body>
</html>

This will correctly adjust the position of the new window

Make sure to adjust values as per your requirements

Top answer
1 of 2
3

Append the parameters to the URL in the following format:

<url>?param1=value1&param2=value...

If you need to pass an array of values then use the same name for the parameter

<url>?arr=value1&arr=value2...

So your URL would look like

domain.com/Controller/Report?name=xyz&date=20


Update:

To receive them in the Action, declare the parameters being passed to the Action.

public ActionResult Report(string name, DateTime date)
{
  ...
}

Read up on Model Binding in ASP.NET MVC

2 of 2
3

I found a better way to pass parameters to the popup window and even to retrieve parameters from it :

In the main page :

var popupwindow;
var sharedObject = {};

function openPopupWindow()
{
   // Define the datas you want to pass
   sharedObject.var1 = 
   sharedObject.var2 = 
   ...

   // Open the popup window
   window.open(URL_OF_POPUP_WINDOW, NAME_OF_POPUP_WINDOW, POPUP_WINDOW_STYLE_PROPERTIES);
   if (window.focus) { popupwindow.focus(); }
}

function closePopupWindow()
{
    popupwindow.close();

    // Retrieve the datas from the popup window
    = sharedObject.var1;
    = sharedObject.var2;
    ...
}

In the popup window :

var sharedObject = window.opener.sharedObject;

// function you have to to call to close the popup window
function myclose()
{
    //Define the parameters you want to pass to the main calling window
    sharedObject.var1 = 
    sharedObject.var2 = 
    ...
    window.opener.closePopupWindow();
}

That's it !

And this is very convenient because:

  • You have not to set parameters in the URL of the popup window.
  • No form to define
  • You can use illimited parameters even objects.
  • Bi-directionnal : you can pass parameters AND, if you want you, can retreive new parameters.
  • Very easy to implement.

Have Fun!

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 78859118 โ€บ passing-a-variable-to-window-open-method
javascript - Passing a variable to window.open method - Stack Overflow
<script> var value = document.getElementById("oper").value; const Gobtn = document.getElementById("Go"); Gobtn.addEventListener("click", function () { window.open("calculate.html", value); }); </script> I'm trying to open "calculate.html" and use the content of the variable named "value" in the "calculate.html" file. Thanks. ... Pass it as a query string value. ... to passing variables i suggest you to look at this similar question: javascript : sending custom parameters with window.open() but its not working