PDA

View Full Version : Can not understand this mis-match of paremeters, please help.



jeddi
Jan 14th, 2010, 12:38 PM
Hi,

I am using the following code to generate a temporary window:

In the js:


var win=null;
function NewWindow(mypage,myname,scroll){
settings='width=850,height=500,top=200,left=50,scrollbars=yes,location=yes,directories=no,status=no, menubar=no,toolbar=no,resizable=yes';
win=window.open(mypage,myname,settings);
if(win.focus){win.focus();}}

function hidestatus(){
window.status=''
return true
}

And in the HTML:


<a href=\"http://www.support-focus.com/check.php?key=$sup_cd2\"
onclick=\"NewWindow(this.href,'','yes','default');
return false\" onfocus=\"this.blur()\" >

This code works , but I don't understand the mis-match or parameters :(

This is passing four param:
onclick=\"NewWindow(this.href,'','yes','default');

But this is only receiving three:
NewWindow(mypage,myname,scroll)

Is the last one (default) not counted for some reason ?


The was able to move the scroll param (yes) out

With this:
onclick=\"NewWindow(this.href,'','default');

and

NewWindow(mypage,myname)

Which now leaves just two params and a default.

Does anyone know why this appears to be unbalanced and also what the
myname param does? ( do I need it ? can I get rid of the '' and the myname ? )

Thanks

Philip M
Jan 14th, 2010, 01:39 PM
function NewWindow(mypage,myname,scroll){

You are passing four parameters, but only receiving three. I do not know what 'default' is supposed to do (if anything) - see what happens if you leave it out.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

jeddi
Jan 14th, 2010, 01:56 PM
Thanks for your reply.

Last time I tried to get rid of the default, the window did not
open. This time it seems OK :)

That just leaves this empty "myname" param.
Is there anyreason why I need to pass this ?

I don't appear to be using it ??

PS
Just tried without the myname field and the
window does not open as a temp. (pop up)
but it just normally links to the page.

Philip M
Jan 14th, 2010, 02:17 PM
That just leaves this empty "myname" param.
Is there anyreason why I need to pass this ?

I don't appear to be using it ??

PS
Just tried without the myname field and the
window does not open as a temp. (pop up)
but it just normally links to the page.

Yes, it is a required part of the syntax. It is a good idea to give the pop-up window a name, e.g. myPop, so that you can reference it again.

rnd me
Jan 14th, 2010, 09:11 PM
Thanks for your reply.

Last time I tried to get rid of the default, the window did not
open. This time it seems OK :)

That just leaves this empty "myname" param.
Is there anyreason why I need to pass this ?

I don't appear to be using it ??

PS
Just tried without the myname field and the
window does not open as a temp. (pop up)
but it just normally links to the page.

it sounds like you omitted the popup settings argument as well (the 'width=850,height=500,top=200,left=50,scrol ...' part)
without it, a new tab/window will be created.

you don't need to pass a name, "" works just fine.

you don't need to specify name to grab a reference to the popup as was suggested beause the return from window.open() gives you a reference:

win=window.open()
in that case, "win" would be you popup window object.

personally, i like to leave the 2nd argument blank.
this keeps open the possibility to pass data across domains to the popup if the need later arises.
if you spend the name on an identifier, you can't use it to hold data...
while the xdomain window.name transport is not often used, it does make it easier to transition to a CDN, HTTPS, etc...

jeddi
Jan 16th, 2010, 06:45 AM
Thanks for your input.

I have ended up with 5 lines of code in my script
and I have stripped down my HTML as well.


var win=null;
function NewWindow(mypage,myname){
settings='width=850,height=500,top=200,left=50,scrollbars=yes,location=yes,directories=no,status=no, menubar=no,toolbar=no,resizable=yes';
win=window.open(mypage,myname,settings);
if(win.focus){win.focus();}}


HTML:


<a href=\"http://www.example.com/index.php?key=$sup_cd2\"
onclick=\"NewWindow(this.href,''); return false\" onfocus=\"this.blur()\" >
<img src=\"http://www.example.com/image_load.php?id=$sup_cd2\"
alt=\"Click to Verify - Before you Buy\" border=\"0\" ></a>

This appears to be working fine..


it sounds like you omitted the popup settings argument as well (the 'width=850,height=500,top=200,left=50,scrol ...' part)
without it, a new tab/window will be created.


Is it OK to have these settings in the javascript like I do rather than passing them over ?


Unless you see a problem with it, I think I'll use this as my default code but
if some clients want to be able to change the window position and size then I could supply a second version that allows the passing of those parameters.

It has been a useful exercise - I know a bit more now :)

Philip M
Jan 16th, 2010, 07:45 AM
Is it OK to have these settings in the javascript like I do rather than passing them over ?


Yes, more than OK. That is how you should do it. :)

Old Pedant
Jan 17th, 2010, 01:41 AM
There *IS* a reason to give a name to a window.

If you do that, then *other* pages from your site can later "find" that same window.

See the page that opened the window has a reference just fine, and so you could pass info to the popup from it.

But suppose you wanted to pass info to (or from) the popup to another "main window"??

You can do that by, later, getting a reference to the window thus:


var popped = window.open( "", "TheName" );

By specifying a blank URL, if a window of that name exists you will get a reference to it without altering it.

[There is a drawback: If the window of that name does *not* exist, then that code will indeed open a new window by that name...so you probably want to *immediately* try to find some data in the opened window and--if it's not there--close the window. There are other tricks you can play to minimize the disruption.]