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

标签 javascript

我在开头说我一般不熟悉 Javascript。我在 HTML 页面中添加了一些简单的内联 JavaScript,以根据是或否弹出窗口来操作 URL。

前提很简单。当页面加载时,会弹出一个窗口(在 onload= 中调用函数),要求用户确认提交。如果他们点击"is",页面将重新加载相同的 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>

最佳答案

经过一番尝试和错误,我发现问题出在使用 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>

关于Javascript 代码不适用于 IE11,但适用于所有其他浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58030319/

相关文章:

javascript - 父 div 滚动控制子 div 滚动事件

javascript - 如何使用 jQuery 的 json2html 将文件内的文本渲染到另一个 html 页面中?

javascript - dropzone.js:上传后显示复选标记和 x 图标

javascript - NodeJS循环遍历字符串并获得准确的输出

javascript - 为什么在 ECMAscript 中函数 <= 函数为真,但 NaN <= NaN 为假

javascript - 使用 enzyme ,如何在组件 react native 中找到子组件

javascript - 使用 Jest 对依赖于 http 调用的代码进行单元测试的最佳方法?

javascript - 如何下载播放音频

javascript - Algolia Search typeahead 实现

javascript - 在javascript中将字节数组转换为字符串