1

I preface this by saying that I am unfamiliar with Javascript in general. I have added some simple inline javascript to an HTML page to manipulate the URL based on a yes or no pop-up window.

The premise is simple. When the page loads there is a pop-up(function is called in the onload=) that asks the user to confirm their submission. If they hit yes, the page reloads the same URL, but with different URL parameters. It seems weird, but the application I am working with just prints HTML(The new parameter will prevent it from reloading the confirm pop-up). The code works on every browser I have tried(Chrome, Firefox, Opera, Edge), but it does not work in IE11. The confirmation pop-up never appears, it just renders the content normally. There are no errors.

I have made sure that the page allows javascript in Internet options

This is my URL format: myurl?conf=false

Here is my code:

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>
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
R Wri
  • 272
  • 1
  • 17
  • Possible duplicate of [Why is indexOf not working in Internet Explorer?](https://stackoverflow.com/questions/3697555/why-is-indexof-not-working-in-internet-explorer) – Chris W. Sep 20 '19 at 14:31
  • `window.location.href = url + hash; var xhr = new XMLHttpRequest(); xhr.open('GET', url + hash, true);` . <-- that seems wrong, navigating and making an xhr request at the same time??? – epascarello Sep 20 '19 at 14:32
  • url.searchparams is also one that isn't supported on IE – Chris W. Sep 20 '19 at 14:32
  • Possible duplicate of [XMLHttpRequest file upload not working in IE11 ](https://stackoverflow.com/questions/38590353/xmlhttprequest-file-upload-not-working-in-ie11) – metatron Sep 20 '19 at 14:33
  • I assume the error console is full of errors – epascarello Sep 20 '19 at 14:33
  • 2
    Possible workaround: drop IE11 support ;) – metatron Sep 20 '19 at 14:36
  • @ChrisW. Interesting, I assumed it had to do with the url manipulations. I'll have to come up with something else. If so much is unsupported I think I am going down the wrong path. – R Wri Sep 20 '19 at 14:36
  • @metatron Trust me, I WISH. It's a little unfortunate when the majority of your customer base is using it... – R Wri Sep 20 '19 at 14:36
  • @epascarello It might be wrong, I do not write javascript outside of this instance, but it works everywhere except IE. Also, the console has no errors, I mentioned that in my post. The page just renders without the javascript doing anything. – R Wri Sep 20 '19 at 14:38

1 Answers1

0

So after some trial and error, I've found out that the issue was with using the URL object.

Using the code from this post

My working 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>
R Wri
  • 272
  • 1
  • 17