我需要使用POST方法发送数据的功能,并在POST发送后,刷新页面。但每次它询问用户"警报需要注意。您确定要关闭此页吗?"。有没有办法只强制刷新而不要求用户许可?现在我正在使用" window.location = window.location.href;"但它无论如何都要求用户。
function clickedNew(file){
var http = new XMLHttpRequest();
http.open("POST", "./script.php", true);
http.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=utf-8");
var params = "fileName=" + file + "&fileNum=" + <?php echo $count; ?>;
http.send(params);
http.onload = function() {
window.location = window.location.href;
}
}
答案 0 :(得分:0)
按Static variables in JavaScript。
中所述,在第一次通话时为您的功能提供新属性然后您可以检查并阻止HTTP [POST]:
function clickedNew(file){
if(this.isRefreshing == "undefined")
{
var http = new XMLHttpRequest();
http.open("POST", "./script.php", true);
http.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=utf-8");
var params = "fileName=" + file + "&fileNum=" + <?php echo $count; ?>;
http.send(params);
this.isRefreshing = "refreshing";
}
else{
this.isRefreshing = "undefined"; // should be able to repeat
}
http.onload = function() {
window.location = window.location.href;
}
}
其他script.js
function instantiateClicked(file){
var insted = new clickedNew();
insted.isRefreshing = "undefined";
insted(file);
}