【问题标题】:location.reload(true); not working in ie11location.reload(true);不在ie11中
【发布时间】:2019-02-14 11:53:38
【问题描述】:

我有一个脚本,当值 >= 100 时重新加载页面问题是 location.reload(true);不在 ie11 中工作,我也尝试过 window.location = self.location.href;但是我遇到了同样的问题,在其他浏览器中效果很好。

$(function () {
if (value < 100) {
    var timer = setInterval(function () {
        $.ajax({
            type: "GET",
            url: $("#ancUrl").attr('href'),
            data: {},
            success: function (msg) {
                console.log("This is msg:" + msg);
                var msgInt = parseInt(msg);
                if (msgInt > value)
                    value = msgInt;
            },
            error: function (err) {
                console.log(err.responseText);
            },
            dataType: "json"
        });
        $("#progress-bar").width(value.toString() + "%");

        if (value >= 100) {
            clearInterval(timer);
            window.location = self.location.href;
        }
    }, 2000);
}

});

【问题讨论】:

  • 试试window.location.href = window.location.href;

标签: javascript jquery ajax internet-explorer


【解决方案1】:

您似乎没有在任何地方定义self,因此您可能在那里遇到错误。除此之外,您还试图将 href 的值分配为 location 的整个值 - 这意味着是一个对象。相反,请尝试:

window.location.href = window.location.href;

【讨论】:

    【解决方案2】:

    尝试将 if 语句移动到成功回调中。

    这样你就可以清除interval到同一个stack并重新加载页面就行了 .

    $(function() {
      if (value < 100) {
        var timer = setInterval(function() {
          $.ajax({
            type: "GET",
            url: $("#ancUrl").attr('href'),
            data: {},
            success: function(msg) {
              console.log("This is msg:" + msg);
              var msgInt = parseInt(msg);
              if (msgInt > value)
                value = msgInt;
              $("#progress-bar").width(value.toString() + "%");
              if (value >= 100) {
                clearInterval(timer);
                window.location = self.location.href;
              }
            },
            error: function(err) {
              clearInterval(timer);
              console.log(err.responseText);
            },
            dataType: "json"
          });
    
        }, 2000);
      }
    });
    

    【讨论】:

      【解决方案3】:

      把if放在success函数中,ajax是异步的,if会立即执行,但是ajax完成后value会发生变化,所以代码可能永远不会到达if语句

      $(function () {
      if (value < 100) {
          var timer = setInterval(function () {
              $.ajax({
                  type: "GET",
                  url: $("#ancUrl").attr('href'),
                  data: {},
                  success: function (msg) {
                      console.log("This is msg:" + msg);
                      var msgInt = parseInt(msg);
                      if (msgInt > value) {
                          value = msgInt;
                          $("#progress-bar").width(value.toString() + "%");
                          if (value >= 100) {
                             clearInterval(timer);
                             location.reload(true);
                           }
                      }
                  },
                  error: function (err) {
                      console.log(err.responseText);
                  },
                  dataType: "json"
              });
      
      
      
          }, 2000);
      }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-03
        • 2014-12-23
        • 2016-09-15
        • 1970-01-01
        • 2019-02-11
        • 2021-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多