Javascript代码不适用于IE11,但适用于所有其他浏览器

时间:2019-09-20 14:28:41

标签: javascript

首先,我说自己不熟悉Javascript作为开头。我已经在HTML页面中添加了一些简单的内联JavaScript,以基于“是”或“否”弹出窗口来操纵URL。

前提很简单。页面加载后,会弹出一个窗口(在onload =中调用该函数),要求用户确认其提交。如果它们是,则页面将重新加载相同的URL,但具有不同的URL参数。看起来很奇怪,但是我正在使用的应用程序仅打印HTML(新参数将阻止它重新加载确认弹出窗口)。该代码适用于我尝试过的每种浏览器(Chrome,Firefox,Opera,Edge),但不适用于IE11。确认弹出窗口永远不会出现,它只是正常显示内容。没有错误。

我确保该页面允许Internet选项中的javascript

这是我的网址格式:myurl?conf = false

这是我的代码:

function checkIfConfirmed() {
  var url_string = window.location.href;
  var url = new URL(url_string);
  var isConfirmed = url.searchParams.get("conf");
  if (isConfirmed == "false") {
    confirmStatus(isConfirmed);
  }
}

function setGetParameter(paramName, paramValue) {
  var url = window.location.href;
  var hash = location.hash;
  url = url.replace(hash, '');
  if (url.indexOf(paramName + "=") >= 0) {
    var prefix = url.substring(0, url.indexOf(paramName + "="));
    var suffix = url.substring(url.indexOf(paramName + "="));
    suffix = suffix.substring(suffix.indexOf("=") + 1);
    suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
    url = prefix + paramName + "=" + paramValue + suffix;
  } else {
    if (url.indexOf("?") < 0)
      url += "?" + paramName + "=" + paramValue;
    else
      url += "&" + paramName + "=" + paramValue;
  }
  window.location.href = url + hash;
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url + hash, true);
  xhr.send();
}

function confirmStatus(isUnconfirmed) {
  if (confirm("Are you sure you want to proceed?")) {
    setGetParameter("conf", "true");
  } else {
    close();
  }
}
<BODY onload="checkIfConfirmed()">
  <!-- The content to be displayed -->
</BODY>

1 个答案:

答案 0 :(得分:0)

因此,在经过反复试验之后,我发现问题出在使用URL对象。

Using the code from this post

我正在使用的javascript:

<script>
function getQueryString() {
          var key = false, res = {}, itm = null;
          // get the query string without the ?
          var qs = location.search.substring(1);
          // check for the key as an argument
          if (arguments.length > 0 && arguments[0].length > 1)
            key = arguments[0];
          // make a regex pattern to grab key/value
          var pattern = /([^&=]+)=([^&]*)/g;
          // loop the items in the query string, either
          // find a match to the argument, or build an object
          // with key/value pairs
          while (itm = pattern.exec(qs)) {
            if (key !== false && decodeURIComponent(itm[1]) === key)
              return decodeURIComponent(itm[2]);
            else if (key === false)
              res[decodeURIComponent(itm[1])] = decodeURIComponent(itm[2]);
          }

          return key === false ? res : null;
}

function setGetParameter(paramName, paramValue)
{
    var url = window.location.href;
    var hash = location.hash;
    url = url.replace(hash, '');
    if (url.indexOf(paramName + "=") >= 0)
    {
        var prefix = url.substring(0, url.indexOf(paramName + "="));
        var suffix = url.substring(url.indexOf(paramName + "="));
        suffix = suffix.substring(suffix.indexOf("=") + 1);
        suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
        url = prefix + paramName + "=" + paramValue + suffix;
    }
    else
    {
    if (url.indexOf("?") < 0)
        url += "?" + paramName + "=" + paramValue;
    else
        url += "&" + paramName + "=" + paramValue;
    }
    window.location.href = url + hash;
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url + hash, true);
    xhr.send();
}

function confirmStatus() {
  var confirmed = getQueryString('conf');
  if(confirmed = "false") {
    if (confirm("Are you sure you want to proceed?")) {
      setGetParameter("conf", "true");
    } else {
      close();
    }
  }
}
</script>