JavaScript : Centering a Pop-up window

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
glenn
Forum Newbie
Posts: 18
Joined: Fri Apr 19, 2002 7:42 am
Location: London, England (UK)

JavaScript : Centering a Pop-up window

Post by glenn »

Hello,

Thanks for the help on the Random Image function, Rob the R, it worked for me :D.

I have been looking around on the net and in my books but I have not found anyting let but iam sure I have seen a function some where. I am looking for a function that will position my pop-up in the center of the page, e.g 50% from the top and 50% from the side.

Heres what I have so far ::

function open_b_s7() {
leftPos = 0
height = 0
if (screen) {
leftPos = screen.width-225
height = screen.width-0
}
window.open("episodes/b_s7.html", "test", "left="+leftPos+", top="+height+", width=200, height=320, toolbar=no, status=no, resizeable=no, menubar=no, scrollbars=no")
}

If any one can help me with this I would be thank fall.


Glenn Curtis.
phpfreak
Forum Commoner
Posts: 30
Joined: Fri Mar 21, 2003 10:28 am
Location: New Jersey,USA
Contact:

Post by phpfreak »

User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post by dstefani »

Here is something I've used without any problems.
Have fun.

- Don

Code: Select all

// In head
<script language="JavaScript">
<!--
function openWindow(theURL,winName,features, myWidth, myHeight, isCenter) &#123;
  if(window.screen)if(isCenter)if(isCenter=="true")&#123;
    var myLeft = (screen.width-myWidth)/2;
    var myTop = (screen.height-myHeight)/2;
    features+=(features!='')?',':'';
    features+=',left='+myLeft+',top='+myTop;
  &#125;
  window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
&#125;
//-->
</script>


// In body
// This example uses a image link

<a href="javascript:;" onClick="openWindow('popup_page.html','','toolbar=no,status=no','750','450','true')">
<img src="images/openMe.jpg" width="253" height="144" border="0" alt="Click to open popup!"></a>
glenn
Forum Newbie
Posts: 18
Joined: Fri Apr 19, 2002 7:42 am
Location: London, England (UK)

Post by glenn »

Thanks for the function but I have told my self that I shouldn't use JavaScript that I don't understand!!

Could anyone explain the above function :?: :?:

Please explain it in a simplest way has you can. I am having a lot of problems with getting my head around JavaScript! I can understand other languages like PHP but JavaScript is just a little different, I think my problem is that no other languages I have seen have dots ( . ) used to space commands!!

Don't take this the wrong way, its just that if iam ever to build websites for a big company (which I hope I will someday) I would think they would need me to understand how JavaScript works!!

Like I say thanks for the function anyway!!

Glenn Curtis
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post by dstefani »

Perfect opportuity to get the Oreilly JavaScript book and figure out how it works. No sarcasim intended. ;-)

- Don
glenn
Forum Newbie
Posts: 18
Joined: Fri Apr 19, 2002 7:42 am
Location: London, England (UK)

Post by glenn »

Whats the ponit??

I have about 4 books on JavaScript and I have read and re-read them again and again and again and again. Plus the fact that I dont like Oriley's books as I think that they dont give a lot of code examples and thats how I tend to learn things (like PHP!).


But it doesn't matter, I may give it another go my-self and when that doesn't work I will just give it a fixed position!


But thanks all the same!!!! :D
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

glenn wrote:Plus the fact that I dont like Oriley's books as I think that they dont give a lot of code examples and thats how I tend to learn things (like PHP!).
They're an excellent reference though - very handy when you do have examples to decipher (like dstefani gave above). However, you could find another reference (probably free) on the web to work out what it's doing... no need to dismiss the example out of hand.

Mac
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Oh, and I think O'Reilly publishes great books!

:P
Post Reply