PDA

View Full Version : Positioning pop-ups


fingerwerks
03-29-2003, 12:14 AM
Hi. I'm very new to coding and have a question about pop-ups. I currently us a code
function main(){
popup=window.open( "filename.htm", "name","width=","height=","menubar=no","resizeable=no","status=no");
}

This is in the <HEAD>

I use a behaviour on a slice to call javascript 'javascript:main();'

That works great for the popup, no problem. My question is that the window pops up in the top left of the screen. I would like it centered, or right aligned. Is this possible? I've seen it done, but really have no idea how. Please help

liorean
03-29-2003, 12:18 AM
You have left and right values as well as width and height. Use screen.availHeight and screen.availWidth and compute where to put the popup.

HairyTeeth
03-29-2003, 01:45 AM
Heres a generic popup function. You can alter things in two places:
1) the divisors that determine the position of teh popup (see comments in script) and,
2) The arguments in the link that opens the popup.


<html>
<head>
<title>Positionable Popup Demo</title>

<script type="text/javascript">
<!-- ;
function oPop(align,url,winName,width,height,parameters){
var leftPos,topPos;
//leftPos = left position;
//topPos = top position;

//change the divisors (eg. 1.25)to move the window around;
if(align=="center"){
leftPos = (screen.availWidth - width) / 2
topPos = (screen.availHeight - height) / 2
}else if(align == "right"){
leftPos = (screen.availWidth - width) / 1.25;
topPos = (screen.availHeight - height) / 4;
}
window.open(url,winName,"width="+width+",height="+height+",left="+leftPos+",top="+topPos + ","+ parameters);
}
// end hide -->
</script>
</head>
<body>

<!-- the link and the parameters should be kept on one line -->
<a href="javascript:void oPop('right','','myWindow','500','200','status,toolbar')">Right-alignedPopup</a>

<br />

<a href="javascript:void oPop('center','somePage.htm','','300','300','status,toolbar,location')">Center-alignedPopup</a>
</body>
</html>

See hope that goes anyway. It worked for me on IE 5.0 and NN4.73 but i would test in other browsers. Also, theres a few other popup management statements that I haven't included, that really should be part of a popup script, but this should be enough for now. Cheers.

tpeck
03-29-2003, 08:16 AM
You can also play around with this popular script:

<script language="javascript" type="text/javascript">
<!--
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',l ocation=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
win=window.open(mypage,myname,settings);}
// -->
</script>


<a href="http://www.popup.html" onclick="NewWindow(this.href,'myname','320','240','yes','center');return false" onfocus="this.blur()">go popup</a>


Terry

fingerwerks
03-29-2003, 09:50 PM
Thanks a lot guys. Those scripts will come in handy.