Forum Moderators: open

Message Too Old, No Replies

msgWindow - variable width and height

I am a javascript idiot

         

fidibidabah

9:46 pm on Jun 4, 2004 (gmt 0)

10+ Year Member



Pretty simple, I have this little msgWindow script that works fine, but I have a few different links that I need to open small windows, and they need to be of different sizes. So instead of making 10 functions, I figure it would be more efficient to make 1 function with varible widths and heights. Here's what I have working now:

function newWindow(file,window) {
var msgWindow;
msgWindow=open(file,width,'resizable=no,width=1337,height=1337');
if (msgWindow.opener == null) msgWindow.opener = self;
return false;
}

Then the link side of it is:
href="javascript:void(0);" onClick="return newWindow('file.html','OMG',);"

Basically, just like the 'file' is a varible that controls which .html file gets loaded into the new window, I want to have a varible for width and height, and I tried the obvious way (mimicking the way the file varible works) and i come up with nothing.

Sorry for the nub question, thanks.

Bernard Marx

11:11 pm on Jun 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's the simplest one.

You can control url, width, height, and optionally give the window a name.
[A window popped to the same name as an already-existing window will just change that window, not open a new one, so it's useful]

The function returns a ref to the popup, so you can store it in a variable (as I have done below).
I have put the URL in the

href
of the link (for non-JS browsers). The
[blue]return false[/blue]
is to stop this link being followed when JS is enabled and a pop opens instead.

<script>
function pop(url,width,height,name)
// optional: last arg
{
var win = window.open(
url,name,"width="+width+",height="+height+"resizable=0"
)

return win
}
</script>

<a href = "page.htm" onclick="pop1=pop('page.htm',200,300,'win1');return false">open pop</a>

BTW, I was interested in this bit in yours:

if (msgWindow.opener == null) msgWindow.opener = self;

This assigns the current window to the opener property of the pop-up. Apparently, someone believes that this isn't the default behaviour anyway. Maybe there are browsers that don't support this property - if so, it's a good idea, so maybe put it back (with the varName changed).

fidibidabah

5:06 am on Jun 5, 2004 (gmt 0)

10+ Year Member



Thanks! That really helps..

I am however still confused out of my mind. I want them all to be the same 'window' so that if someone clicks a different one, the current window resizes instead of it creating a new one. I thought giving them all the same name would do this, but no beans. Any ideas?

Bernard Marx

9:38 am on Jun 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Usually, you would just give them the same name. Trouble is, the window will always stay at its initial size. Is this the behaviour you're getting?

You could have a go at resizing the window, inside the function, using the local var:

win.resizeTo(width,height)

1. This may need to be on a v. short timed delay for some browsers.
2. I don't actually know whether these dimensions will give the same size window as when used in open (inner / outer width).

Have a go

fidibidabah

5:39 pm on Jun 5, 2004 (gmt 0)

10+ Year Member



Oh my god it works.

Well you're right, they are slightly different dimensions, but I'll mess with it :)

Edit: Woo, I even figured something out myself! msgWindow.focus(); after everything else in the function causes the pop to be in focus again, which is nice. Otherwise when the user clicks it changes dimensions but stays minimized :D

Forever greatful.