Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the page file name from the address bar

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.

like image 474
Boardy Avatar asked Dec 13 '11 22:12

Boardy


People also ask

How do I get my URL from the address bar?

You can use Location object property of the Window object to get these details. href => This will return the entire URL displaying in the address bar. host => This will return the hostname and port of the URL in address bar. hostname => This will return only the hostname of the URL in address bar.


1 Answers

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.

like image 180
ShankarSangoli Avatar answered Sep 29 '22 18:09

ShankarSangoli