Get the page file name from the address bar
Try this:
var pageName = (function () {
var a = window.location.href,
b = a.lastIndexOf("/");
return a.substr(b + 1);
}());
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; }
https://developer.mozilla.org/en/DOM/window.location
alert(location.pathname)
If you don't want the leading slash, you can strip it out.
location.pathname.substring(1)
Try this
location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
location.pathname
gives the part(domain not included) of the page url. To get only the filename you have to extaract it using substring
method.