Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the name of an html page in Javascript?

I have an html page and I would like inside the html page to retrieve the name of the html document via Javascript. Is that possible?

e.g. name of html document = "indexOLD.html"

like image 562
programmer Avatar asked May 17 '13 14:05

programmer


2 Answers

var path = window.location.pathname; var page = path.split("/").pop(); console.log( page ); 
like image 177
Daniel Aranda Avatar answered Sep 28 '22 22:09

Daniel Aranda


Current page: It's possible to do even shorter. This single line sound more elegant to find the current page's file name:

var fileName = location.href.split("/").slice(-1);  

or...

var fileName = 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; } 
like image 23
Hugolpz Avatar answered Sep 28 '22 22:09

Hugolpz