Reputation: 489
I need to get the extension of the URL, for example(.xhtml) using jQuery?
Upvotes: 5
Views: 4641
Reputation: 490333
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();
}
Upvotes: 8
Reputation: 1506
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.
Upvotes: 6