簡體   English   中英

Javascript代碼不適用於IE11,但適用於所有其他瀏覽器

[英]Javascript code does not work on IE11, but works on all other browsers

我首先說我一般不熟悉 Javascript。 我在 HTML 頁面中添加了一些簡單的內聯 javascript 以根據是或否彈出窗口 Z044B8C742CBD96FFBF23 操作 URL

前提很簡單。 當頁面加載時,會有一個彈出窗口(在 onload= 中調用函數),要求用戶確認他們的提交。 如果他們點擊是,頁面重新加載相同的 URL,但具有不同的 URL 參數。 看起來很奇怪,但我正在使用的應用程序只打印 HTML(新參數將阻止它重新加載確認彈出窗口)。 該代碼適用於我嘗試過的所有瀏覽器(Chrome、Firefox、Opera、Edge),但在 IE11 中不起作用。 確認彈出窗口永遠不會出現,它只是正常呈現內容。 沒有錯誤。

我已確保該頁面在 Internet 選項中允許 javascript

這是我的 URL 格式: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 object 上。

使用這篇文章中的代碼

我的工作 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM