url - Display page name in javascript? -
how add location.href.split('/').pop()
html document display page name? (not whole url) display page name only.
example: if page "www.example.com/whaterver/mypage.html" display "mypage".
what full script? new javascript , found online don't know whole code. me out?
i stick in function in case need reuse elsewhere in code. split page name @ end , take first element:
function getpagename(url) { return url.split('/').pop().split('.')[0]; }
you can pass in actual url:
var pagename = getpagename('www.example.com/whaterver/mypage.html'); // mypage
or, using location.href
:
var pagename = getpagename(location.href); // mypage
i might inclined return if there no match *.html
, here's revised function returns null if there isn't match:
function getpagename(url) { var pagename = url.split('/').pop().split('.')[0]; return pagename.length > 0 ? pagename : null; }
Comments
Post a Comment