Firefox não retorna Javascript corretamente

2 respostas
feandrad

Uso uma função Javascript para adicionar options em um select. No Chrome funciona normal, mas no Firefox por uma fração de segundo vejo que a option foi adicionada, mas o navegador navega para uma página com o HTML abaixo, impedindo que continue utilizando a página.

<html>
    <head></head>
    <body>

        false

    </body>

</html>
Só para esclarecer eu não criei nenhuma página Html assim, o Firefox está exibindo o retorno da função não sei porquê. Alguém poderia me explicar o que está acontecendo? Porque só no Firefox isso acontece?

Segue o código da minha função e a chamada:

function addOption(fonte, destino) {

    select1 = document.getElementById(fonte);
    select2 = document.getElementById(destino);

    if (select1.options.length > 0 && select1.selectedIndex != -1) {
        // adiciona todos os elementos selecionados do select1
        // para o select2
        while (select1.selectedIndex != -1) {
            var selectedIndex = select1.selectedIndex;

            var listText = select1.options[selectedIndex].text; // ome(name) do
            // Elemento
            var listValue = select1.options[selectedIndex].value; 
            var listTitle = select1.options[selectedIndex].title;
            // Verifica se o elemento selecionado  foi adicionado anteriomente
            for (k = 0; k < select2.options.length; k++) {
                if (select2.options[k].value == listValue) {
                    // select1.options[selectedIndex] = null;
                    return true;
                }
            }
            // envia o elemento do select1 para o 2
            select2.options[select2.options.length] = new Option(listText,
                    listValue);
            select2.options[select2.options.length - 1].title = listTitle;
            select1.options[selectedIndex].selected = false;

        }
    }

    return false;
}
Chamada da função, onde epesqregular e pesqgrupo são os id dos selects em questão:
<a href="javascript:addOption('epesqregular', 'pesqgrupo')"> 
    <img src="../swf/insere.GIF" border="0" alt="inserir" title="Inserir" /> 
</a>

2 Respostas

feandrad

Ninguém pode me ajudar?

Luiz_Augusto_Prado
feandrad:
Uso uma função Javascript para adicionar options em um select. No Chrome funciona normal, mas no Firefox por uma fração de segundo vejo que a option foi adicionada, mas o navegador navega para uma página com o HTML abaixo, impedindo que continue utilizando a página.
<html>
    <head></head>
    <body>

        false

    </body>

</html>
Só para esclarecer eu não criei nenhuma página Html assim, o Firefox está exibindo o retorno da função não sei porquê. Alguém poderia me explicar o que está acontecendo? Porque só no Firefox isso acontece?

Segue o código da minha função e a chamada:

function addOption(fonte, destino) {

    select1 = document.getElementById(fonte);
    select2 = document.getElementById(destino);

    if (select1.options.length > 0 && select1.selectedIndex != -1) {
        // adiciona todos os elementos selecionados do select1
        // para o select2
        while (select1.selectedIndex != -1) {
            var selectedIndex = select1.selectedIndex;

            var listText = select1.options[selectedIndex].text; // ome(name) do
            // Elemento
            var listValue = select1.options[selectedIndex].value; 
            var listTitle = select1.options[selectedIndex].title;
            // Verifica se o elemento selecionado  foi adicionado anteriomente
            for (k = 0; k < select2.options.length; k++) {
                if (select2.options[k].value == listValue) {
                    // select1.options[selectedIndex] = null;
                    return true;
                }
            }
            // envia o elemento do select1 para o 2
            select2.options[select2.options.length] = new Option(listText,
                    listValue);
            select2.options[select2.options.length - 1].title = listTitle;
            select1.options[selectedIndex].selected = false;

        }
    }

    return false;
}
Chamada da função, onde epesqregular e pesqgrupo são os id dos selects em questão:
<a href="javascript:addOption('epesqregular', 'pesqgrupo')"> 
    <img src="../swf/insere.GIF" border="0" alt="inserir" title="Inserir" /> 
</a>
<head>  
 <script>
  

// ver fonte da solução: 
// http://stackoverflow.com/questions/128923/whats-the-effect-of-adding-return-false-to-an-onclick-event

function addOption(fonte, destino) {  
  
    select1 = document.getElementById(fonte);  
    select2 = document.getElementById(destino);  
  
    if (select1.options.length > 0 && select1.selectedIndex != -1) {  
        // adiciona todos os elementos selecionados do select1  
        // para o select2  
        while (select1.selectedIndex != -1) {  
            var selectedIndex = select1.selectedIndex;  
  
            var listText = select1.options[selectedIndex].text; // ome(name) do  
            // Elemento  
            var listValue = select1.options[selectedIndex].value;   
            var listTitle = select1.options[selectedIndex].title;  
            // Verifica se o elemento selecionado  foi adicionado anteriomente  
            for (k = 0; k < select2.options.length; k++) {  
                if (select2.options[k].value == listValue) {  
                    // select1.options[selectedIndex] = null;  
                    return true;  
                }  
            }  
            // envia o elemento do select1 para o 2  
            select2.options[select2.options.length] = new Option(listText,  
                    listValue);  
            select2.options[select2.options.length - 1].title = listTitle;  
            select1.options[selectedIndex].selected = false;  
  
        }  
    }  
  
    return false;  
} 

 </script>  
</head>   
<body style="margin: 10px;">

<select id="epesqregular">
  <option>aaa1</option> 
  <option>aaa2</option> 
</select>

<select id="pesqgrupo">
  <option>Apple</option>
  <option>Orange</option>
  <option>Pineapple</option>
  <option>Banana</option>
</select>

	<a href="#" onclick="addOption('epesqregular', 'pesqgrupo')">Click here !</a> ---
	<a href="javascript:addOption('epesqregular', 'pesqgrupo')"> inserir </a>  

</body>
Criado 21 de novembro de 2014
Ultima resposta 24 de nov. de 2014
Respostas 2
Participantes 2