最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to stop an automatic refresh using JS - Stack Overflow

programmeradmin7浏览0评论

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...)

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...)

Share Improve this question edited Feb 28, 2014 at 11:11 Chen-Tsu Lin 23.3k16 gold badges56 silver badges65 bronze badges asked Feb 28, 2014 at 10:33 GeoGeo 492 silver badges8 bronze badges 2
  • Do you actually have element with id result? – dkasipovic Commented Feb 28, 2014 at 10:36
  • Yes, in my HTML code, I have, but it was the "return false" that I was missing, thank you too ! – Geo Commented Feb 28, 2014 at 11:11
Add a ment  | 

2 Answers 2

Reset to default 2

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;
}

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.

发布评论

评论列表(0)

  1. 暂无评论