如何使用jquery读取url中的单词?

时间:2012-11-15 20:14:40

标签: javascript jquery

我如何使用jquery / javascript读取第一个单词,例如端口号后面的“something”一词?

这是网址,我希望得到端口号后的第一个字

http://www.aaa.com:1081/something/arap/con.html

$(document).ready(function() {
    var currentUrl= window.location.pathname;
    alert (currentUrl);
}); 

3 个答案:

答案 0 :(得分:1)

又快又脏:

window.location.pathname.split('/')[1]

但你可能想要使用这样的东西:https://github.com/allmarkedup/jQuery-URL-Parser

答案 1 :(得分:0)

$(document).ready(function() {
    var pathArray = window.location.pathname.split( '/' );
   //Then access the different parts by the parts of the array, like
    var secondLevelLocation = pathArray[0];

});

答案 2 :(得分:0)

尝试:

$(document).ready(function() {
    var currentUrl= window.location.pathname.split('/')[1];
    alert (currentUrl);
});