Click to See Complete Forum and Search --> : Positioning a JavaScript-launched window with a percentage


Wozza
07-21-2003, 01:04 AM
Title says it all, really. Can I launch a window with JavaScript, and position it with a percentage value, rather than with a pixel value (i.e. 50% rather than 100px)?
And, just to be difficult, can I position it at 50%-50px? (half the screen width, minus half the window width, equals perfectly centered)

Cheers in advance-
Wozza

gil davis
07-21-2003, 07:21 AM
The window.open() command does not take percentages for top and left. You can calculate the position and concatenate the answers:
var ht = 200;
var wd = 300;
var features = "top=" + (screen.height - ht) * .5 + ",left=" + (screen.width - wd) * .5 + ",width=" + wd + ",height=" + ht;
window.open(theURL, theName, features);

Wozza
07-21-2003, 08:36 AM
G'day, many thanks for the reply. I'm not sure exactly how to use this code, though (I'm still very new to javascript). Any chance you could put it into the context of the function below?

function lswitch()

{
window.open ('switch.html', 'switch', config='height=400,width=580,top=200,left=200')
}

Cheers =)

gil davis
07-21-2003, 03:51 PM
function lswitch() {
var ht = 400;
var wd = 580;
var features = "top=" + (screen.height - ht) * .5 + ",left=" + (screen.width - wd) * .5 + ",width=" + wd + ",height=" + ht;
window.open ('switch.html', 'switch', features);
}

Wozza
07-21-2003, 10:00 PM
You, sir, are a legend =) I was trying to make the variables seperate, then stick them in the function somehow.

Many thanks once again for all the help.

-Wozza

Wozza
07-22-2003, 03:09 AM
ah...

just to be a pain in the arse, how would I give this window scrollbars?

Again, more sincerely than ever,
Cheers.

-Wozza

gil davis
07-22-2003, 06:57 AM
Add
features += ",scrollbars=yes";
before the window.open().

See http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/open_0.asp for all other open window features.