开发者

How to get the extension of an url

I need to get the extensio开发者_如何转开发n of the URL, for example(.xhtml) using jQuery?


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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜