0

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];
    }
  }
};
John Hoven
  • 4,085
  • 2
  • 28
  • 32
Aaron Lowe
  • 33
  • 8
  • PS I also can't get all the code to fit inside a code block for some unknown reason (to me). – Aaron Lowe Aug 20 '16 at 01:52
  • 2
    What's the value of `window.location.search.substring(1)`? – user94559 Aug 20 '16 at 01:54
  • Also, you mentioned Firefox... does that mean you see different behavior on other browsers? – user94559 Aug 20 '16 at 01:58
  • All my debug extensions are for Firefox so not sure of the exact value window.location.search.substring(1) returns, but outputting it through alert(); returns an empty string also. So, the behaviour is the same in Firefox and Chrome (both most recent versions). And just checked its exact value in Firefox - it is an empty string. – Aaron Lowe Aug 20 '16 at 14:46
  • I've uploaded an example to http://www.atomz.host-ed.me/#?l=en. I'm testing the website for different spoken languages. If you click one of the flags it is supposed to change the browser title but it doesn't. – Aaron Lowe Aug 20 '16 at 14:59

2 Answers2

1

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).)

user94559
  • 59,196
  • 6
  • 103
  • 103
  • Thanks. That solved the problem. Had to also add an event handler to reload the page on hash change to make it work in my case. `window.onhashchange = function() { location.reload(); }` – Aaron Lowe Aug 20 '16 at 23:57
  • @AaronLowe Instead of refreshing the page and doing this work on page load, you could just do the work when the hash changes. – user94559 Aug 21 '16 at 00:08
  • The reason there is a hash in the url is to link to the current page with a variable setting the language. Because the whole page has to be reloaded anyway as all the content changes it's not so much hassle. I could achieve the same by having loads of ids and change the elements contents dynamically, but that seems overkill. – Aaron Lowe Aug 21 '16 at 05:15
  • No doubt there are better ways of doing it but I'm not a web designer - just an amateur dabbler. I want to get the whole concept working first before thinking about making it more efficient. – Aaron Lowe Aug 21 '16 at 05:17
0

Try using window.location.hrefor document.URL for getting the complete URL of current page. More about them :

  1. https://developer.mozilla.org/en-US/docs/Web/API/Document/location
  2. https://developer.mozilla.org/en-US/docs/Web/API/Document/URL
hello512
  • 379
  • 4
  • 16