1

I request the javascript guru masters again!

Okay this is probably simple, blowing my mind atm.

Example: http://example.com/page.php

var path = location.pathname;

Returns: /page.php

I would like it to not include the first / so it would return page.php

Thank you guru masters!

jemiloii
  • 24,594
  • 7
  • 54
  • 83

3 Answers3

5

You just need to use one of the several substring functions.

var path = location.pathname.substr(1);

Update: substr is now a deprecated method, so you should use slice instead.

var path = location.pathname.slice(1);
Keith
  • 984
  • 6
  • 9
  • 1
    No problem. Here is an interesting link to tell you the difference (in your case, not necessary) between the methods that do the same thing: substr(), substring(), slice(): http://stackoverflow.com/questions/3745515/what-is-the-difference-between-substr-and-substring – Keith Feb 21 '14 at 20:19
2
var path = location.pathname.substring(1);
kennebec
  • 102,654
  • 32
  • 106
  • 127
0

You can use replace() function also, but it replaces the all found forward slashes in path.

var path = location.pathName.replace("/","");
Suman Bogati
  • 6,289
  • 1
  • 23
  • 34