Pregunta

I'm using javascript to create a file explorer in my website. I have a function wich read each file from my website and change them into a caracters chain with a chain.split() for each file. Then, in the array created, I search words that I take from a form. And then, with an innerHTML, I rewrite my HTML page with the answers.

It works, but, when the page is rewrite, it automaticly refresh, and I lose all my search reults... I tried to stop refresh with window.stop(), document.execCommand('stop'), and it's still refresh...

Here my form :

<form name="recherche" onsubmit="javascript:maFonction()">
<INPUT class="finder" type="text" name="maRecherche" placeholder="Enter your search"/>
<input class="press" type="submit" name="search" value="Search"/>
<p style="margin-left:5%">It may take five secondes...</p>
</form>

And here, the writing part of my JS function :

var mesResultats = "";
    if (bin > 0)
    {
        a = 0;
        mesResultats += 'your search <u><b>' + words + '</u></b> can be found here : <BR><BR>';
        for (var i = 0; i < mesLiens.length; i++)
        {
            if (mesLiens[i] != mesLiens[i-1] )
            {
                var monLien = '<div style="margin-left:5%; margin-right:5%; text-align:justify;"><a href="http://noaadc-ttmcr101:8888/HelpOnLine/web_uk/' + mesLiens[i] + '">' + mesTitres[a] + '</a>' + '<BR></div>';
                mesResultats += monLien + '<hr>';
            }
            a++;
        }

    }
    else 
    {
        var monLien = '<a href="http://noaadc-ttmcr101:8888/HelpOnLine/web_uk/index.html">Homepage</a>'; 
        mesResultats += 'No answer corresponding to your search <u><b>' + words + '</u></b>... ' + monLien + '</div>';
    }
    elemnt = document.getElementById("result");
    elemnt.innerHTML = mesResultats;

If anyone have an idea of how to keep my search results, thank you !

(PS : I can't show you with a link...)

¿Fue útil?

Solución

Add return false into the onsubmit event, to don't refresh the page.

HTML :

<form name="recherche" onsubmit="return myFunction();">

Javascript :

function myFunction(){
   return false;
}

Otros consejos

In case someone has the same situation as me, the application was using <meta http-equiv="refresh" content="3"> and was refreshing the content every 3 seconds. The solution for me was to execute window.stop(); directly from the console.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top