posted
I need to make a pop up window appear in the center of the screen. I have the window appearing how I want, but not in the correct position.
In the header of the source-of-link page:
code:
<script language="JavaScript"><!-- function P(url,h,w) { // (url,height,width) var p = "height=" + h + ",width=" + w + ",scrollbars=yes"; window.open(url,"",p); } // --> </script>
The link in the body of the source-of-link page:
code:
<a href="javascript:P('smallwindow.htm',175,400)">Link to pop up window</a>
Can I get it to appear in the center of the screen?
Disclaimer: If you take this opportunity for some macho strutting where you say I'm doing everything wrong and you could do it so much better, I'll mock you severely. Thank you.
posted
I'm hoping Zevlag will see it. He's already the guru.
Yes, I know. I'm taking out the primary social incentive for helping someone with programming. In exchange for the help and in place of the opportunity for macho strutting, though, this answerer'll get my approval and a hug at KamaCon. And hey! Last time, Jon Boy got a dollar. All good things.
posted
look up in your friendly Javascript (us macho programmers know it better as ECMAScript ) reference about "window positioning", "screen positioning", or something like that.
Or you can look at a site that has a center popup, and look at the code.
posted
Hm. I've used the following concept before:
(screen.availHeight - <height>)/2
I believe the screen.availHeight and screen.availWidth properties are present across most systems. And you could probably do a simple if (screen) to verify that the OS has that property available.
You can then set the "top" attribute of window.open to the result of the function above, and set the "left" attribute of window.open to the result of screen.availWidth - <width>/2. I believe "top" and "left" are universal nowadays, but I seem to recall "screenX" and "screenY" being used for some reason in the old days.
posted
You don't need to use moveTo(). As I said in my last post, you can actually specify positioning using the "top" and "left" attributes of window.open().
Posts: 37449 | Registered: May 1999
| IP: Logged |
var initialwidth,initialheight var ie5=document.all&&document.getElementById var ns6=document.getElementById&&!document.all
function loadwindow(url,width,height){ if (!ie5&&!ns6) window.open(url,"","width=width,height=height,scrollbars=1",winProp) else{ var winleft=(screen.width-500)/2 var winUp=(screen.height-525)/2 document.getElementById("dwindow").style.display='' document.getElementById("dwindow").style.width=initialwidth=width+"px" document.getElementById("dwindow").style.height=initialheight=height+"px" document.getElementById("dwindow").style.left=winleft document.getElementById("dwindow").style.top=winUp document.getElementById("cframe").src=url } }
posted
I actually did something completely different and started from scratch, because I found a reference of how to do that.
For general information, this is what I ended up doing:
code:
<SCRIPT LANGUAGE="JavaScript">
function popUp(URL) { day = new Date(); id = day.getTime(); eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0, statusbar=0,menubar=0,resizable=0,width=420, height=200,left = 326,top = 332');"); }
</script>
code:
<A HREF="javascript:popUp('smallwindow.htm')">Open the Popup Window</A>
posted
Zev, why isn't that NS5 compliant? I notice you're restricting it to NS6, but I can't figure out what element there wouldn't work from NS5 on.
------
Kat, if you modify the code as I recommended, so that "top" and "left" are variables rather than static attributes, it will let you assign the window to the middle of the page regardless of the user's actual resolution. If you use your code, that window will not be perfectly centered in many browsers.
posted
Tom, I honestly don't remember, I remember running into bug with something. *sigh* its been a while since I've really had to use it, I just grabbed it out of my source code snippet database (something I started keeping a few years back, has been EXTREMLY helpful) It probably could use some updating.
Posts: 2102 | Registered: Dec 2000
| IP: Logged |
posted
In all fairness, I use popups for a number of things, especially data verification/alert windows for data entry screens. While they blow major chunks when used for sales, or to draw attention to pointless information, they make nice dialog boxes.
posted
A technique I quite like for many uses is in-page popups. In other words, divs in the page at a closer z level, controlled using the DOM, usually.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
Update for anyone who cared: The request for this page was fourth-hand by the time it got to me, and the original need was for something completely different. *sigh* I need a Dr. Pepper.
Posts: 26077 | Registered: Mar 2000
| IP: Logged |