Javascript在不询问用户的情况下刷新页面

Javascript refreshing page without asking user

本文关键字:情况下 刷新 用户 Javascript      更新时间:2023-09-26

我有一个函数,我需要用POST方法发送数据,并在发送POST后刷新页面。但每次它都会询问用户"警报需要注意。你确定要关闭此页面吗?"。有没有办法在不征求用户许可的情况下强制刷新?现在我使用的是"window.location=window.locationhref;",但它还是会询问用户。

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

在第一次调用时为函数指定新属性,如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); 
}