Javascript 如何只重新加载一次页面而不重复加载
在使用JQuery提交一些数据后,我只有以下代码可以重新加载页面一次 要重新加载页面的代码: 更新:此处的url没有以“?”结尾,因为它具有参数值 例如:http://localhost:49208/UserView.aspx?id=12Javascript 如何只重新加载一次页面而不重复加载,javascript,jquery,Javascript,Jquery,在使用JQuery提交一些数据后,我只有以下代码可以重新加载页面一次 要重新加载页面的代码: 更新:此处的url没有以“?”结尾,因为它具有参数值 例如:http://localhost:49208/UserView.aspx?id=12 这里的问题是页面重新加载不会停止 它不会停止重新加载的原因是您没有更改url的条件;因此,如果if语句是真的,它将一次又一次地发生 如果要重新加载页面,只需使用window.location.reload 试试这个逻辑 if (url.indexOf('?'
这里的问题是页面重新加载不会停止 它不会停止重新加载的原因是您没有更改url的条件;因此,如果if语句是真的,它将一次又一次地发生
如果要重新加载页面,只需使用window.location.reload 试试这个逻辑
if (url.indexOf('?') == -1) {
url = url + '?';
location = '?';
location.reload(true);
}
最简单的方法是添加一个标志变量,这样javascript就可以检查之前是否重新加载了页面
var url = window.location.href; // get the current url of page into variable
if (url.indexOf('?') > -1) { // url has a '?'
if(url.indexOf('reloaded') < 0){ // url does not have the text 'reloaded'
url = url + "&reloaded=true"; // add the word 'reloaded' to url
window.location = url; // "reload" the page
}
}
如果只想重新加载页面一次,请使用以下方法:
if(!window.location.hash) {
window.location = window.location + '#loaded';
window.location.reload();
}
添加一个新的查询字符串键值,该键值表示&alreadyReloaded=yes,并在javascript中检查它
if(!window.location.hash) {
window.location = window.location + '#loaded';
window.location.reload();
}
if(!window.location.hash)
{
window.location = window.location + '#loaded';
window.location.reload();
}