jQuery 单击按钮刷新页面,然后调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16581414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:23:54  来源:igfitidea点击:

Refresh page on button click and after that call an function

javascriptjquery

提问by Hyman Php

I am trying to refresh an web page on button click and after page refresh call an function. But its reverse is not happening i.e. first call the function and after that refresh the page.

我正在尝试在单击按钮时刷新网页,并在页面刷新后调用函数。但它的反向不会发生,即首先调用该函数,然后刷新页面。

This is what I have tried:

这是我尝试过的:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
    function restartall(){
        $('#text').append('hello');
    }
</script>
<a href="javascript:document.location.reload();">
<button onclick="restartall()">Referash Map</button></a>
<div id="text"></div>

please solve this query.

请解决这个查询。

回答by rahul maindargi

use below code

使用下面的代码

<body onload="onrefresh();">

<a href="javascript:myfun();">

function myfun(){
    window.location.search += '&reload=true';
}
function onrefresh(){
 if(gup("reload")=='true'){
    // DO whatever want to do. 
  }
}


function gup( name ){
    name = name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");  
    var regexS = "[\?&]"+name+"=([^&#]*)";  
    var regex = new RegExp( regexS );  
    var results = regex.exec( window.location.href ); 
    if( results == null )
        return "";  
    else
      return results[1];
}

回答by Sudarshan

<a href="javascript:abc();"><script>function restartall() {
    $('#text').append('hello');
}

function abc() {
    document.location.reload();
    restartall();

}</script>

回答by Wiktor

When you reload page you're loosing JavaScript call stack. You would have to append some parameter to URL or use cookie.

当您重新加载页面时,您会丢失 JavaScript 调用堆栈。您必须将一些参数附加到 URL 或使用 cookie

回答by Rahul Dasgupta

try this

尝试这个

<script>
    function restartall(){
        $('#text').append('hello');
        document.location.reload();
    }
</script>

<input type="button" onclick="restartall()" value="Referash Map" />
<div id="text"></div>