Pergunta

Eu tenho um seletor suspenso no lugar e eu preciso mudá-lo de modo que a target = "_ blank" para que ele abre uma nova aba.

Aqui está o código atual:

<SCRIPT TYPE="text/javascript">
<!--
function dropdown(mySel)
{
var myWin, myVal;
myVal = mySel.options[mySel.selectedIndex].value;
if(myVal)
   {
   if(mySel.form.target)myWin = parent[mySel.form.target];
   else myWin = window;
   if (! myWin) return true;
   myWin.location = myVal;
   }
return false;
}
//-->
</SCRIPT>

<div id=countryselector>
    <FORM
        ACTION="../cgi-bin/redirect.pl"
        METHOD=POST onSubmit="return dropdown(this.gourl)">
        <SELECT NAME="gourl">
            <OPTION VALUE="">Select a Country...
            <OPTION VALUE="http://google.com">USA
            <OPTION VALUE="http://google.ca">Canada
        </SELECT>
        <INPUT TYPE=SUBMIT VALUE="Go">
    </FORM>
</div>

Agradecemos antecipadamente

Foi útil?

Solução

function dropdown(mySel) {
    var myVal = mySel.options[mySel.selectedIndex].value;
    if (myVal) {
        if (mySel.form.target) {
            window.open(myVal, mySel.form.target, '_attributes_');
        } else {
            window.location.href = myVal;
        }
    }
    return false;
}

A lista de _attributes_ pode ser encontrada aqui para Mozilla ou aqui para IE . Existem algumas diferenças em algumas das opções disponíveis, por isso é melhor rever ambas as listas.

Você também pode deixar o terceiro parâmetro fora da chamada de função e deve se comportar como target="_blank" em seu <form>:

// behaves as if you submitted <form ... target="_blank">:
window.open(myVal, mySel.form.target);

Aqui está um exemplo usando um conjunto de _attributes_ como documentado nos links fornecidos para abrir uma janela de um tamanho específico ea posição com partes específicas do UI suprimida:

// this opens a window that is 400 pixels by 300 pixels
// it is positioned 100 pixels from the top and the left
// it will have no statusbar, no menu but the new window will have a toolbar:
window.open(myVal, mySel.form.target,
    'height=300,width=400,top=100,left=100,statusbar=0,menu=0,toolbar=1');
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top