How to get the extension of an url

我需要使用jQuery获取URL的扩展名,例如(.xhtml)?


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.


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();
}
链接地址: http://www.djcxy.com/p/70248.html

上一篇: 正则表达式来匹配图片网址

下一篇: 如何获得一个URL的扩展