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; } 

demo


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -