Skip to content

Instantly share code, notes, and snippets.

@sammydigits
Created December 24, 2014 18:17
Show Gist options
  • Select an option

  • Save sammydigits/91fa6906bd66ddb718d4 to your computer and use it in GitHub Desktop.

Select an option

Save sammydigits/91fa6906bd66ddb718d4 to your computer and use it in GitHub Desktop.
Exact filename from URL
function getFileName() {
//this gets the full url
var url = document.location.href;
//this removes the anchor at the end, if there is one
url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
//this removes the query after the file name, if there is one
url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));
//this removes everything before the last slash in the path
url = url.substring(url.lastIndexOf("/") + 1, url.length);
//return
return url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment