Use window.open():

<a onclick="window.open(document.URL, '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');">
  Share Page
</a>

This will create a link titled Share Page which opens the current url in a new window with a height of 570 and width of 520.

Answer from citruspi on Stack Overflow
🌐
W3Schools
w3schools.com › jsref › met_win_open.asp
Window open() Method
window.open("https://www.w3schools.com"); Try it Yourself » · More examples below. The open() method opens a new browser window, or a new tab, depending on your browser settings and the parameter values.
🌐
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › jsref › met_win_open.asp.html
Window open() Method - w3schools
window.open("http://www.w3schools.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400"); Try it Yourself »
🌐
W3Schools
w3schools.com › jsref › prop_win_opener.asp
Window opener Property
addeventlistener() alert() atob() blur() btoa() clearInterval() clearTimeout() close() closed confirm() console defaultStatus document focus() frameElement frames history getComputedStyle() innerHeight innerWidth length localStorage location matchMedia() moveBy() moveTo() name navigator open() opener outerHeight outerWidth pageXOffset pageYOffset parent print() prompt() removeEventlistener() resizeBy() resizeTo() screen screenLeft screenTop screenX screenY scrollBy() scrollTo() scrollX scrollY sessionStorage self setInterval() setTimeout() status stop() top Window Console
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();
🌐
P2hp
w3.p2hp.com › jsref › met_win_open.asp
Window open() Method - W3Schools
The open() method opens a new browser window, or a new tab, depending on your browser settings and the parameter values.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-window-open-method
JavaScript window.open() Method - GeeksforGeeks
August 5, 2025 - If an empty string is provided then it will open a blank new tab. windowName: It can be used to provide the name of the window. This is not associated with the title of the window in any manner. It can accept values like _blank, _self, _parent, etc. windowFeatures: It is used to provide features to the window that will open, for example, the dimension and position of the window.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Window › open
Window: open() method - Web APIs | MDN
The open() method of the Window interface loads a specified resource into a new or existing browsing context (that is, a tab, a window, or an iframe) under a specified name. ... A string indicating the URL or path of the resource to be loaded. If an empty string ("") is specified or this parameter ...
🌐
Educative
educative.io › answers › what-is-the-windowopen-method-in-javascript
What is the window.open() method in JavaScript?
If no URL is indicated, a blank browser window will be opened. name: The name parameter is like the target attribute.
Top answer
1 of 2
2

The simple answer is yes. But make it a function that calculates those values when called. In other words:

let helpDimensions=function (w,h,l,t) {
return "width="+(w||findWidth())+", height="+h||findHeight()+", left="+(l||findLeft())+", top="+(t||findTop());
}
//this lets you pass a width, height, left, and top to override the calculated dimensions.
/*you will need to establish a findWidth(), findHeight(), findLeft(), and findTop functions or include them inline like:
return "width="+window.innerWidth*0.8+", height="+window.innerHeight*0.8+", left="0.1*window.innerWidth+", top="+window.innerHeight*0.1;
*/

/*then just perform function call like this:
window.open(url, title, helpDimensions()); to calculate the correct dimensions each time
*/

Also, establish a click handler on an element like this:

$("#someelement").on("click", function (e) {
e.preventDefault();
window.open("test.html","test", helpDimensions());
});

using jquery to set the onClick attribute of an existing element is a bad idea.
Also make sure that all of this is enclosed in an Immediate Invoked Injection Function or IIIF like this:

$(function () {
//define your helpDimensions function to calculate dimensions dynamically
//define your click event handler
});

//this ensures that it is not called until everything is loaded.

Hope this helps.
Here’s a great website for calculating dimensions and working with the window reference object to ensure that your content goes into the correct window (which can be reused if needed):

xtf.dk

Center a new popup window even on dualscreen with javascript

function PopupCenter(url, title, w, h) { // Fixes dual-screen position Most browsers Firefox var dualS...

2 of 2
3

I have a window.open command as follows:

$('#documentation_href').attr('onclick', "window.open('../help.php#Sale_Records','Help','width=1800,height=900,top=100,left=200')");

This works fine on my desktop but I’m sure the width and height will be too big for some computers so I would like to vary the parameters depending on the screen height.

I’ve successfully got some code to calculate the ideal values and have placed them into a parameter - the values vary depending on the screensize.

I’ve set this parameter as follows:

help_dimensions = 'width=' + help_width + ',height=' + help_height + ',left=' + help_left + ',top=' + help_top;

But I would like to know how I can use the parameter help_dimensions in my window.open command instead of the hardcoded values.

@nrgfusion

🌐
W3Schools
w3schools.com › js › js_window.asp
W3Schools.com
All global JavaScript objects, functions, and variables automatically become members of the window object.
🌐
Gitbooks
makersinstitute.gitbooks.io › html-css-js › content › fifth-chapter › the-windowopen-method.html
The Window.open() Method · HTML + CSS + JS
The 3 parameters are: A URL so that the window contains a specific HTML document. ... A set of options for the window's appearance, including height and width. For example, let's say I created a page called mytest.htm. It might be as simple as this: <HTML> <HEAD> <TITLE>Test Page </TITLE> </HEAD> ...
🌐
Arai-a
arai-a.github.io › window-open-features
window.open with features
features parameter of window.open interacts with where the newly created browsing context is opened.
🌐
W3Schools
w3schools.am › jsref › met_win_open.html
Window open() Method - W3Schools
The open() method opens a new browser window, or a new tab, depending on your browser settings and the parameter values. Tip: Use the close() method to close the window. ... var myWindow = window.open("", "MsgWindow", "width=200,height=100"); myWindow.document.write("<p>This is 'MsgWindow'.
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();

}
🌐
JavaScript.info
javascript.info › tutorial › frames and windows
Popups and window methods
If there is no 3rd argument in the open call, or it is empty, then the default window parameters are used.