0

How can I get the value in the url using jquery in my wordpress theme footer.php?

I'm using the following code to get url

var url = window.location.href; 

And the result I get is www.example.com/2016/ and the value I want to get from this url is 2016 what is the next step to get the number 2016 from the url?

Wint
  • 39
  • 1
  • 10
  • Sorry, wrong duplicate - here is a better one: http://stackoverflow.com/questions/16984943/how-to-get-the-directory-part-of-current-url-in-javascript – mplungjan Jan 23 '17 at 09:20

1 Answers1

0

Assuming an input of http://www.example.com/2016/ (as it would appear if you use window.location to retrieve it) then you can split() the URL by the / character and retrieve the values from the resulting array. Try this:

var url = 'http://www.example.com/2016/'; // use window.location
var urlArray = url.split('/');
var year = urlArray[3];

console.log(year);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339