open new window

A

Andy

Hi,

I have this code in the head of the page:

script language="JavaScript" type="text/JavaScript">
<!--

function goTo(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>

and this bit in the body section:

<form name="form1" method="post" action="">
<select name="select" onChange="goTo('self',this,0)">

Simple put I want the new page to open in a new window, I assumed that
changing the 'self' bit to 'blank' would work but all I get are JavaScript
undefined error messages.

Does anybody have any other ideas as to what I could try ?

Thanks
 
L

Lasse Reichstein Nielsen

Andy said:
script language="JavaScript" type="text/JavaScript">

The language attribute is deprecated and can safely be omitted

This HTML comment starter can also safely be omitted.
function goTo(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");

Yikes. Bad use of eval. You will probably never need to use eval, as
there are better alternatives.

Just use:
function goTo(tObj,selObj,restore) {
tObj.location=selObj.options[selObj.selectedIndex].value;
...
and
<form name="form1" method="post" action="">
<select name="select" onChange="goTo('self',this,0)">
onchange="goTo(self,this,false);"


Simple put I want the new page to open in a new window, I assumed that
changing the 'self' bit to 'blank' would work but all I get are JavaScript
undefined error messages.

This code is only built to open in existing windows.
Does anybody have any other ideas as to what I could try ?

function goToNew(winName,selObj,restore) {
var win = window.open(selObj.options[selObj.selectedIndex],winName);
if (restore) {selObj.selectedIndex=0;}
return win;
}

called as, e.g.:
onchange="goToNew('_blank',this,false);"

/L
 
M

Michael Winter

I have this code in the head of the page:

script language="JavaScript" type="text/JavaScript">

The language attribute is deprecated, and the presence of the type
attribute makes it redundant. Remove it.

Script hiding is a practice that is now obsolete. Remove the SGML comment
delimiters.
function goTo(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");

Why are you using eval()? I don't see why you can't just make targ an
object reference and then write:

targ.location.href = selObj.options[ selObj.selectedIndex ].value;

Notice that there is no need to convert the value property to a string.
The form control values are *always* returned as strings.
if (restore) selObj.selectedIndex=0;
}
//-->

Remove this delimiter along with the first.
</script>

and this bit in the body section:

<form name="form1" method="post" action="">
<select name="select" onChange="goTo('self',this,0)">

To work with the changes above, change the call to

goTo(self,this,false)

I changed the last argument to false because it makes more sense to pass
booleans when evaluating booleans.
Simple put I want the new page to open in a new window, I assumed that
changing the 'self' bit to 'blank' would work but all I get are
JavaScript undefined error messages.

That's because "self" is an object reference to the current window
(equivalent to window.self or window), whereas "blank" isn't defined at
all.
Does anybody have any other ideas as to what I could try ?

A call to window.open(), perhaps?

Two things to note:

1) Don't perform navigation actions on the "change" event.
2) Avoid new windows completely, if possible.

If I use my mouse wheel to change the value, the event will fire every
time even though I probably haven't chosen a destination yet. Similarly,
if I make a mistake when choosing a value, I will not get an opportunity
(unless I'm very quick) to correct it. Use a button instead.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top