Get Current Page Name


/ Published in: JavaScript

Get current page name (file name) with extension (if any).


Copy this code and paste it in your HTML
  1. function GetCurrentPageName(){
  2. var url = window.location.pathname;
  3. var urlLength = url.length;
  4. var lastSlashLocation = url.lastIndexOf('/');
  5. //Make sure the last char in the path isn't '/'
  6. if (urlLength - lastSlashLocation <= 1)
  7. url = url.substring(0,lastSlashLocation);
  8. var pageName = url.substring(url.lastIndexOf('/') + 1);
  9. return pageName;
  10. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.