...

View Full Version : function openWindow() question



BossJames
07-15-2004, 08:56 PM
I am using "function openWindow() to open three seperate, same sized page/windows.

I had no problems using this method opening two page/windows but when i added the third, the page/window "events_btn_03.html" will not open, instead page/window "events_btn_04.html opens. this happens eventho each of these pages are named seperately & uploaded to the server as such.

the file name for each page/window is different: events_btn_02.html, events_btn_03.html, events_btn_04.html, but the function only opens the 02 & 04 page/window.

here is the code in the homepage (http://www.chevelles.net/oregon/) where the first link (events button) begins:

function openWindow(file,name) {
if (screen) {
var topPos = screen.height /2 -240;
var leftPos = screen.width /2 -200;
}
var newWin = window.open(file,name,'width=200,height=500,left='+leftPos+',top='+topPos+''); newWin.focus();

this leads to a secondary page/window that has link buttons for the years 2002, 03, & 04, the code here is:

<script language="javascript"><!--
function openWindow() {
if (screen) {
var topPos = screen.height /2 -250;
var leftPos = screen.width /2 -100;
}
var newWin = window.open('events_btn_02.html','02events','width=200,height=500,left='+leftPos+',top='+topPos+'');
newWin.focus();
}
// -->
</script>

<script language="javascript"><!--
function newWindow() {
if (screen) {
var topPos = screen.height /2 -250;
var leftPos = screen.width /2 -100;
}
var newWin = window.open('events_btn_03.html','03events','width=200,height=500,left='+leftPos+',top='+topPos+'');
newWin.focus();
}
// -->
</script>

<script language="javascript"><!--
function newWindow() {
if (screen) {
var topPos = screen.height /2 -250;
var leftPos = screen.width /2 -100;
}
var newWin = window.open('events_btn_04.html','04events','width=200,height=500,left='+leftPos+',top='+topPos+'');
newWin.focus();
}
// -->
</script>

the function code in the "events_btn_03.html" page is:

function openWindow() {
if (screen) {
var topPos = screen.height /2 -250;
var leftPos = screen.width /2 -100;
}
var newWin = window.open('events_btn_03','03events','width=200,height=500,left='+leftPos+',top='+topPos+'');
newWin.focus();
}
// -->
</script>


thank you for any suggestions or solutions:
james
800-875-4276 PST
:)

Roy Sinclair
07-15-2004, 09:36 PM
It looks like your second page is in error, you've taken a script (the openWindow script), modified it to not use parameters like it does on your original page and then inserted a second copy of that script on the page using the same name altering the replaced parameters again.

I suggest you replace both of those adulterated copies of the original script with the original script again and then fix the calls to the original script to supply the URL and window names again. Note that if you want separate windows to open make sure you provide unique window names, if you want whichever link is clicked to open in the same window, use the same window name in your calls to the openWindow function.

Looking at your website I see the function is present but it isn't even called on your homepage so I sense you've got a real disconnect here in understanding how that script even works and how to use it.

jamescover
07-15-2004, 09:36 PM
:eek:

jamescover
07-15-2004, 09:48 PM
sorry, I didn't see the focus bit...:


<script>
<!--

function newWin(url,winName,props){
var myWin = window.open(url,winName,"width=400,height=400,top="+((screen.height-400)/2)+",left="+((screen.width-400)/2));
myWin.focus();
}


//-->
</script>

<a href="javascript:newWin('events_btn_02.html','win1','props');void(0);">Open New Window</a>

<a href="javascript:newWin('events_btn_03.html','win2','props');void(0);">Open New Window</a>

<a href="javascript:newWin('events_btn_04.html','win3','props');void(0);">Open New Window</a>




http://www.ekigroup.com/javascript/winGen.html


-james

jamescover
07-15-2004, 10:50 PM
:thumbsup:

This one is more practical for multiple windows--values defined in the handlers...


<script>
<!--

function newWin(picName,winName,w,h,top,left){

var largePic = window.open(picName,winName,'width='+w+',height='+h+',top='+((screen.height-h)/2)+',left='+((screen.width-w)/2));

}

//-->
</script>

<a href="javascript:newWin('http://www.yourDomain.com/yourPage.html','myWin','300','300','top','left');void(0);">Open New Window</a>



-james

SpiritualStorms
07-16-2004, 06:42 AM
Here's what i suggest:



function launch_Pad( addy, title, features )
{
var lion;

lion= window.open( addy, title, features )

lion.focus();
}


All of the above creates a real basic template that can be reused as many times as you like. That's the beauty of variables, and functions.

If you need to call the above, or to actually activate the above, you would do something very similar to the below:



<a href="#" onclick="launch_Pad( 'something.html' ,'somethingtitle' , 'top=200px,left=300,height=200px,width=300px' )";> Link_One </a>
<br>
<a href="#" onclick="launch_Pad( 'anothersomething.html' ,'secondtitle' , 'top=200px,left=300,height=200px,width=300px' )";> Link_Two </a>
<br>
<a href="#" onclick="launch_Pad( '3rdsomething.html' ,'3rdTitletitle' , 'top=200px,left=300,height=200px,width=300px' )";> Link_Three </a>
<br>


I will presume that the above makes sense to you?

How it works is real simple: you create a function to seperate a certain number of commands from the rest of the page, if you will. With the function, you create 3 variables that are basically understood as parameters, or arguments, but they are essentially, storage boxes for the holding, or housing, of content, or what is termed values. The difference between a parameter, and a variable is that you dont use the key word var when you declare a parameter. But over, and beyond this, they are a lot a like.

At any rate, once you have your local variable inside your function, you define your variable, so in this sense, we say lion=window.open(). We do this so that we can use the variable for the application, or the accessing of additional methods, or properties. But notice how the open() method has what i would like to call a clamp, since through the clamp, the method can hold additional data. This is why we have, lion=window.open(addy, title, features). All we have done is pass the variables from the function launch_pad() onto the open() method, and bingo, you have now a means by which you can send data to its desired location.

At any rate, we simply pass the paramters of the function, unto the open() method, and wola, you have now a practical little tool by which you can create individual pop-ups. Since each time you call a function, the parameters of that function will contain different pieces of data, or as was stated, values, there's no need to worry about whether or not the function will launch the same window. As long as the values of the parameters are different, you should get different windows. Thus, although you may have a single function, it can be used like 10 different times, for like 10 different pop-ups.

Does any of this make sense to you?

glenngv
07-16-2004, 08:00 AM
Using functions is nice but not in the case of that launch_Pad example. No offense meant, but it looks like overkill to use a function.

Your launch_Pad function is almost just a duplicate of window.open. The arguments are the same and you only have window.open inside the function.

<a href="#" onclick="launch_Pad( 'something.html' ,'somethingtitle' , 'top=200px,left=300,height=200px,width=300px' )";> Link_One </a>

<a href="#" onclick="window.open( 'something.html' ,'somethingtitle' , 'top=200px,left=300,height=200px,width=300px' )";> Link_One </a>

Both achieve the same effect, so the latter is better since you don't have the extra function. If you want to focus to the opened window, you can put it like this:

<a href="#" onclick="window.open( 'something.html' ,'somethingtitle' , 'top=200px,left=300,height=200px,width=300px' ).focus()";> Link_One </a>

But if you want to do some other stuff inside the function before the actual opening of the window, like getting the left and top coordinates to center the popup to the screen and whatever, then use a function.

jamescover
07-16-2004, 08:07 AM
...drives the scroll to the top of the document:

<a href="#" onclick="window.open( 'something.html' ,'somethingtitle' , 'top=200px,left=300,height=200px,width=300px' )";> Link_One </a>


anyway...

<a href="javascript:window.open('something.html','somethingtitle','top=200px,left=300,height=200px,width=300p x');void(0);"> Link_One </a>


-james

SpiritualStorms
07-16-2004, 12:05 PM
Glen:

Using functions is nice but not in the case of that launch_Pad example. No offense meant, but it looks like overkill to use a function.


GOOD point. Don't know why simple things slip through my mind. But yea, your right.

glenngv
07-16-2004, 01:02 PM
...drives the scroll to the top of the document:

<a href="#" onclick="window.open( 'something.html' ,'somethingtitle' , 'top=200px,left=300,height=200px,width=300px' )";> Link_One </a>


anyway...

<a href="javascript:window.open('something.html','somethingtitle','top=200px,left=300,height=200px,width=300p x');void(0);"> Link_One </a>


-james
<a href="something.html" target="somethingtitle" onclick="window.open( this.href, this.target , 'top=200px,left=300,height=200px,width=300px' ).focus();return false;"> Link_One </a>

SpiritualStorms
07-17-2004, 06:48 AM
<a href="something.html" target="somethingtitle" onclick="window.open( this.href, this.target , 'top=200px,left=300,height=200px,width=300px' ).focus();return false;"> Link_One </a>


So what does the return false here mean?

gohankid77
07-17-2004, 04:36 PM
I use it to ensure that the original page doesn't go all of the way back to the top of the document when the window opens (useful for super long pages). I don't know the logic behind it, but that is my purpose for including it.

BossJames
07-19-2004, 12:43 AM
you're right, i am trying to get my head around javascript but can't seem to, reading paul wilson's beginning javascript & tom negrino javascript for the WWW 3rd edition.

what confuses me is i have used this script as iis to call up two seperate windows, (the 2002 & 2003 button window/page) with no problems. but when i simply duplicated the same code i was using on the main (page 2) button page/window & changed the url & name to find the new page "2004", the script started finding the first "2002" & third "2004" page & couldn't call the 2003 page any more, even the 2003 button called up the 2004 events button window/page. confusing. anyway, i am going to print out the code, take your suggestions & see if i can fix it.
thank you for your time.
james

BossJames
07-19-2004, 12:53 AM
if i understand you correctly, i do not need to have this code on my homepage at all. correct?

function openWindow(file,name) {
if (screen) {
var topPos = screen.height /2 -240;
var leftPos = screen.width /2 -200;
}
var newWin = window.open(file,name,'width=200,height=500,left='+leftPo s+',top='+topPos+''); newWin.focus();

BossJames
07-19-2004, 12:55 AM
i only understand a limited amount. thanks for the explination.
james

BossJames
07-19-2004, 12:57 AM
i only understand a limited amount. thanks for the explination. bty the code you provided me does not show up in my window. i am using IE 5.1 for the mac.
thanks
james

Willy Duitt
07-19-2004, 12:58 AM
I use it to ensure that the original page doesn't go all of the way back to the top of the document when the window opens (useful for super long pages). I don't know the logic behind it, but that is my purpose for including it.

return false cancels the href.

If you use href="#" without returning false the page will reload and look for an anchor which in this case has no name and thus you are back at the top of the reloaded page....

If you use Glenn's suggestion of properly assigning a location to the href and then pass that value to your window open function, users with javascript disabled will still be able to follow the link. Albiet as a regular link but at least it doesn't fail and they leave your page, never to return because your links do not work....

......Willy

BossJames
07-19-2004, 01:14 AM
sorry about the #2 post. in looking over the code, it looks to me that i should not have any function code present for this script in any of the "events_btn_02.html", "events_btn_03.html","events_btn_04.html", windows, perhaps it is causing the comfusion.

is the naming of the windows, 02events, 03events, 04events too similar for the script?

james



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum