How would I add location.href.split('/').pop()
to a html document to display the page name? (not the whole URL) I would like to display the page name ONLY.
example: if the page was "www.example.com/whaterver/mypage.html" it would display "mypage".
What would be the full script? I am new to javascript and I found this online but I don't know the whole code. could anyone help me out?
Note: If you do
.split('.')
like other solutions instruct, you will miss the base names from many URLs.You can find the last forward slash and search ahead for the first
.
,?
,&
or#
to catch variations of URLs. This is the equivalent of PHP's basenameand use it like so
of
Results:
JSBin Demo