Adrian van Vliet
Adrian van Vliet

Reputation: 1521

Getting the current script executing filename in javascript

Okay, I've been searching for this for way too long already... I'm trying to find out how I can return the filename of the page that's running an included javascript, from inside that javascript.

I can easily do this in PHP using $_SERVER['SCRIPT_FILENAME'], but in Javascript this seems to be a lot harder.

Do I really need to substring it from location.href or is there a more efficient way? Could jQuery help?

Upvotes: 3

Views: 3635

Answers (2)

Avisek Chakraborty
Avisek Chakraborty

Reputation: 8309

Remove the Url Parameters

function getFileName()
{
  var url = window.location.pathname;
  var lastUri = url.substring(url.lastIndexOf('/')+1);
  if(lastUri.indexOf('?')!= -1)
     return lastUri.substring(0,lastUri.indexOf('?'));
  else
     return lastUri;
}

Upvotes: 4

kennebec
kennebec

Reputation: 104770

var url=location.href;
return url.substring(url.lastIndexOf('/')+1)

Upvotes: 2

Related Questions