3

I need to get the extension of the URL, for example(.xhtml) using jQuery?

Wazy
  • 8,695
  • 10
  • 53
  • 97
Harry
  • 473
  • 4
  • 10
  • 22

2 Answers2

7

jQuery doesn't come into play here.

If you can guarantee your URL ends with an extension...

var path = window.location.pathname,
    ext = path.substr(path.lastIndexOf('.') + 1);

...or...

var ext = window.location.pathname.split('.').pop();

...otherwise this will return the full path. You could fix that by making it a bit more verbose...

var path = window.location.pathname.split('.'),
    ext;

if (path.length > 1) {
    ext = path.pop();
}
alex
  • 460,746
  • 196
  • 858
  • 974
5

You could take a look at this SO post - it describes how to get the current URL using JQuery. Getting the extension would be relatively simple after that:

$(document).ready(function() {
    var ext = window.location.pathname.split('.').pop();
});

Other SO posts also show how to use the path.

Community
  • 1
  • 1
JW8
  • 1,456
  • 4
  • 21
  • 36