Yes you can, to some extent by wrapping it in function call. What I typically do is to have a utility function which I can call upon whenever required.
Something like this:
popOpen: function (url, target, height, width) {
var newWindow, args = "";
args += "height=" + height + ",width=" + width;
args += "dependent=yes,scrollbars=yes,resizable=yes";
newWindow = open(url, target, args);
newWindow.focus();
return newWindow;
}
You can further reduce the parameters by making it an object like:
popOpen: function (params) {
var newWindow, args = "";
args += "height=" + params.height + ",width=" + params.width;
args += "dependent=yes,scrollbars=yes,resizable=yes";
newWindow = open(params.url, params.target, params.args);
newWindow.focus();
return newWindow;
}
And you can call it like this:
var param = { url: '...', height: '...', width: '...' };
popOpen(param);
Or,
var param = new Object;
param.url = '...';
param.height = '...';
popOpen(param);
Answer from Abhitalks on Stack OverflowJavascript window.open() passing variables
window.open parameters as a variable
How can I pass a parameter to a window.open command
Javascript window.open confusing parameters? - Stack Overflow
Videos
Hi, I'm trying to pass two variables to an html page by opening the page using window.open("myfile.html"), I know this: window.open("myfle.html", value) works for passing one variable but it doesn't work for multiple variables. I tried query string but I couldn't make it work. Thanks.
Yes you can, to some extent by wrapping it in function call. What I typically do is to have a utility function which I can call upon whenever required.
Something like this:
popOpen: function (url, target, height, width) {
var newWindow, args = "";
args += "height=" + height + ",width=" + width;
args += "dependent=yes,scrollbars=yes,resizable=yes";
newWindow = open(url, target, args);
newWindow.focus();
return newWindow;
}
You can further reduce the parameters by making it an object like:
popOpen: function (params) {
var newWindow, args = "";
args += "height=" + params.height + ",width=" + params.width;
args += "dependent=yes,scrollbars=yes,resizable=yes";
newWindow = open(params.url, params.target, params.args);
newWindow.focus();
return newWindow;
}
And you can call it like this:
var param = { url: '...', height: '...', width: '...' };
popOpen(param);
Or,
var param = new Object;
param.url = '...';
param.height = '...';
popOpen(param);
The way you are trying is not possible. You might want to do this:
var myparameters = 'width=' + w + ', height=' + h + ', ' + 'left=' + wleft + ', top=' + wtop + ', ' + tools;
win3 = window.open(u, n, myparameters);
Fiddle: http://jsfiddle.net/aBR7C/
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...
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
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 inwindowFeatureswill 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.
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
According to the API specifications of Window.open(), you specify the window features in the 3rd parameter. Therefore, you need to set the 2nd parameter, otherwise whatever you specify in the 2nd parameter will be interpreted as the window name.
you can pass _blank instead of myWindow.
I don't know much about asp.net, but looks like you have a javascript problem. Isn't document.getElementById("hlUser") an <a> element? It has no value attribute.
If you want to get the text element inside the <a> you could query the text property, or innerHTML.
<a id="hlUser">somevalue</a>
document.getElementById('hlUser').text will return somevalue
UPDATE:
Having that <a> inside a repeater makes it impossible to know with confidence any id attribute inside it beforehand. I'd reccomend passing the <a> element, or this.text itself , or even the value using the same server-side script to the function.
UPDATE 2: I see this question might be a duplicate of How do I pass a variable using window.open()?
Just change the & to a +:
function OpenWindow() {
var toUsername = document.getElementById("hlUser");
window.open("ChatWindow.aspx?username=" + toUsername.value, "ChatWindow", "width=350,height=250");
return null;
}
At this level of code, you're working in Javascript rather than VB, and VB's use of & for concatenation is an oddity among programming languages.