Get the page file name from the address bar

后端 未结 7 1189
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 15:32

I was wondering if it would be possible to get the page name from the address bar using jquery or javascript. I know this can be done using PHP but don\'t really want to as

7条回答
  •  渐次进展
    2020-12-09 15:49

    Current page: The single line sound more elegant to find the current page's file name:

    location.href.split("/").slice(-1)
    

    or

    location.pathname.split("/").slice(-1)
    

    This is cool to customize nav box's link, so the link toward the current is enlighten by a CSS class.

    JS:

    $('.menu a').each(function() {
        if ($(this).attr('href') == location.href.split("/").slice(-1)){ $(this).addClass('curent_page'); }
    });
    

    CSS:

    a.current_page { font-size: 2em; color: red; }
    

提交回复
热议问题