javascriptdecodeuricomponent

JS decodeURIComponent returns empty string in Firefix (most recent)


Got the following code from StackOverflow. It's supposed to parse the variables in the URL, but when I debug the value of sURLVariables in the for loop its value is always empty. Any ideas why?

var getUrlParameter = function getUrlParameter(sParam) {
  var sPageURL = decodeURIComponent(window.location.search.substring(1)), 
      sURLVariables = sPageURL.split('&'), sParameterName, i;

  for (i = 0; i < sURLVariables.length; i++) {
    sParameterName = sURLVariables[i].split('=');

    if (sParameterName[0] === sParam) {
      return sParameterName[1] === undefined ? true : sParameterName[1];
    }
  }
};

Solution

  • The code you're using looks for the "search" part of the URL (the query string), but the page you're using doesn't have any query string. The URLs on your page look like http://www.atomz.host-ed.me/#?l=en. Everything after the hash (#) is part of the URL fragment.

    Instead of window.location.search.substring(1), use window.location.hash.substring(2).

    (Or get rid of the question mark at the beginning of the URL fragment and use window.location.hash.substring(1).)