0

I have function which I need to send data with POST method, and after the POST has been sent, to refresh page. But every time it asks user "An alert requires attention. Are you sure you want to close this page?". Is there way to just force refresh without asking user for permission? Right now I'm using "window.location = window.location.href;" but it asks user anyway.

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

1 Answers1

0

Give new property to your function on first call as described in Static variables in JavaScript.

Then you can check and prevent 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;
            }
        }

Other script.js

function instantiateClicked(file){
    var insted = new clickedNew();
    insted.isRefreshing = "undefined";
    insted(file); 
}
Community
  • 1
  • 1
EpiGen
  • 70
  • 6
  • If I copy paste this code it never sends Post because isRefreshing is not declared anywhere. Should I have more code or what? – Antonio Stipić Jun 27 '15 at 14:20
  • Yep, first you need to instantiate your function instead of simply calling it. Look in the link I provided. You need to change the way you use your function. – EpiGen Jun 27 '15 at 14:42