29

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 it is only a html website.

I.e. if the address is www.mywebsite.com/hello.htm how do I get the hello.htm part out of the address.

Thanks for any help you can provide.

Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
Boardy
  • 35,417
  • 104
  • 256
  • 447

7 Answers7

61

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.

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • this is great - does exactly what I needed (fetch document name) - thanks – Luke Mar 07 '13 at 15:30
  • 1
    Thanks for your answer.. But if URL ends with `/` then result will be null. – Sariban D'Cl Apr 22 '16 at 12:44
  • @Sariban D'Cl If there is no document name displayed in the URL then it returns NULL simply because there is no document being shown. Developers usually simplify URLs using [Mod_Rewrite](https://stackoverflow.com/questions/4529552/how-to-simplify-the-url?noredirect=1&lq=1) on Apache – Thielicious Jun 13 '17 at 11:33
55

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)
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
5

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; }
Hugolpz
  • 17,296
  • 26
  • 100
  • 187
4

Try this:

var pageName = (function () {
        var a = window.location.href,
            b = a.lastIndexOf("/");
        return a.substr(b + 1);
    }());
austincheney
  • 1,097
  • 7
  • 8
2

The Location object is your friend:

var pageName = location.pathname.substring(1);

http://www.w3schools.com/jsref/obj_location.asp

Phil Klein
  • 7,344
  • 3
  • 30
  • 33
1

document.URL.match(/[^\/]+$/);

Just a simple alternative.

document.URL returns the document's location.
.match() is a method that filters a string using Regular Expression.
/[^\/]+$/ fetches the rest of the string starting after the last occurring slash /.

demo

Thielicious
  • 4,122
  • 2
  • 25
  • 35
0

I've had an issue where i needed to remove the querystring parameters (?) and/or anchor (#) tags. I did this:

var path = window.location.pathname.toLowerCase();
// find the end of path prior the Query and Anchor designations. strip off everything after the ? or #.  
//      if the ? is first, then use ?, else use the #, else use the string length.
// then replace all \ with /
// then split it into an array based on /
var pagePathAry = path.substr(0, path.indexOf("?") > -1 && path.indexOf("?") < path.indexOf("#") ? path.indexOf("?") : path.indexOf("#") > -1 ? path.indexOf("#") : path.length).replace("\\", "/").split("/");
// get the folder it's in
var subFolder = pagePathAry[pagePathAry.length - 2];
// get the page name
var pageName = pagePathAry[pagePathAry.length - 1];
Remus
  • 61
  • 7