This is topic Javascript question for those with so much knowledge they have nothing to prove :) in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=025421

Posted by katharina (Member # 827) on :
 
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.

[ June 24, 2004, 12:26 PM: Message edited by: katharina ]
 
Posted by katharina (Member # 827) on :
 
[Smile] Anyone?
 
Posted by Hobbes (Member # 433) on :
 
Well if you rule out macho strutting in a programming thread, do you really expect to get any responses?

[Razz] [Wink]

Hobbes [Smile]
 
Posted by Erik Slaine (Member # 5583) on :
 
*wanders into thread*
[Confused]
*leaves*
 
Posted by katharina (Member # 827) on :
 
I'm hoping Zevlag will see it. [Wink] 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. [Smile]

[ June 24, 2004, 12:30 PM: Message edited by: katharina ]
 
Posted by Hobbes (Member # 433) on :
 
A hug at KamaCon? Well then forget since I probably can't make it to KamaCon. [Cry]

Hobbes [Smile]
 
Posted by Bokonon (Member # 480) on :
 
look up in your friendly Javascript (us macho programmers know it better as ECMAScript [Smile] ) 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.

-Bok
 
Posted by Hobbes (Member # 433) on :
 
window.moveTo(x,y)

Once you open the window, then move, that's all I know how to do, I'm not sure you can open it and locate it at the same time.

Hobbes [Smile]
 
Posted by TomDavidson (Member # 124) on :
 
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.

[ June 24, 2004, 12:38 PM: Message edited by: TomDavidson ]
 
Posted by Bokonon (Member # 480) on :
 
Here we go. This was the first result for Javascript windows positioning.

You want to use the moveTo() function.

-Bok
 
Posted by Hobbes (Member # 433) on :
 
*cough*

Hobbes [Smile]
 
Posted by katharina (Member # 827) on :
 
Okay, I figured it out.

Thank you!!!! Y'all are great. [Smile] My hearty approval for the esoteric knowledge. Thanks. [Smile]
 
Posted by Hobbes (Member # 433) on :
 
Can we strut now?

Hobbes [Smile]
 
Posted by Bokonon (Member # 480) on :
 
Hey bub, I was totally posting typing it when you were posting... Plus I gave her a reference site.

[Smile]

-Bok
 
Posted by katharina (Member # 827) on :
 
Only if your ego desperately needs to, sweetie.

---

Yes, you did. Thank you so much. [Smile] Hatrack can be a source of just about all necessary knowledge. Thanks. [Smile]

[ June 24, 2004, 12:39 PM: Message edited by: katharina ]
 
Posted by Hobbes (Member # 433) on :
 
Nah, I have so much experience writting code, and perfecting hash tbale look-ups I don't need to strut for my ego.

Hobbes [Smile]
 
Posted by TomDavidson (Member # 124) on :
 
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().
 
Posted by Zevlag (Member # 1405) on :
 
I personally use the following function:
code:
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
}
}

<a href="javascript:loadwindow('poopup.html',600,400)" >


 
Posted by katharina (Member # 827) on :
 
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>



[ June 24, 2004, 12:43 PM: Message edited by: katharina ]
 
Posted by Zevlag (Member # 1405) on :
 
Much the same as I do. But I like mine better, gives a bit more control, and is nicer to multiple browsers. [Wink]

[ June 24, 2004, 12:47 PM: Message edited by: Zevlag ]
 
Posted by Bob the Lawyer (Member # 3278) on :
 
My cat's breath smells like cat food.
 
Posted by katharina (Member # 827) on :
 
*puts head in hands* So close!
 
Posted by TomDavidson (Member # 124) on :
 
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.

[ June 24, 2004, 12:48 PM: Message edited by: TomDavidson ]
 
Posted by Zevlag (Member # 1405) on :
 
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.
 
Posted by ludosti (Member # 1772) on :
 
Pop up windows make baby Jesus cry

[Wink]
 
Posted by TomDavidson (Member # 124) on :
 
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.

[ June 24, 2004, 12:55 PM: Message edited by: TomDavidson ]
 
Posted by ludosti (Member # 1772) on :
 
Yeah, pop-ups have their legitimate uses. It's just their over and misuse that have given them a bad rap.

[ June 24, 2004, 01:01 PM: Message edited by: ludosti ]
 
Posted by advice for robots (Member # 2544) on :
 
ludosti: [Big Grin]

BtL: [Big Grin]

Humor after my own heart.
 
Posted by fugu13 (Member # 2859) on :
 
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.
 
Posted by katharina (Member # 827) on :
 
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.
 
Posted by Zevlag (Member # 1405) on :
 
Hmm, me too, and the fridge here at work is empty...
 


Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2