Jquery Url Split并获得中等价值

时间:2014-05-17 08:12:34

标签: javascript jquery

我有一个网址www.xyz.com/partyevent/zalen

有没有办法获取url mid值,即“partyevent”并在之前和之后吐出部分。我希望将中间值“ partyevent ”用于将类添加到正文中。

 $(document).ready(function () { 
var url = window.location.pathname;
var count = url.match(new RegExp("/", 'g'));
var urlsplit = url.split("/");
var page_class = urlsplit[count.length];

alert(page_class);
    $('body').addClass(page_class);
});

感谢您的帮助..

1 个答案:

答案 0 :(得分:1)

试试这个:

$(function () { 
    var url = window.location.pathname;
    var urlsplit = url.split("/"); // this returns array of ["","partyevent",zalen]
    var page_class = urlsplit[1]; // you need not bother about count when u need the word just after the domain

    alert(page_class);
    $('body').addClass(page_class);
});