Get file name in javascript

How to get the file name of the page? When you need to read the file name of the current…

Novembre 25, 2010

How to get the file name of the page? When you need to read the file name of the current page in javascript you can use the document.location.href string and parse it to find the filename, here is the script that strips away the other chars and keep only the script file name:

function getFileName() {
	var url = document.location.href;
	url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
	url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));
	url = url.substring(url.lastIndexOf("/") + 1, url.length);
	return url;
}

Another usefull function is the one that get the parameters from the url.

Author

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

Comments on “Get file name in javascript”

There are 2 thoughts

  1. Jeff ha detto:

    Most excellent! Thanks!

Comments are closed