I want to get the my exact location (only PHP page) using javascript, I'm currently using window.location
to get the exact url for example: localhost/empc/myfolder/functions.php
what is the code if I want to get only functions.php
as a result? Is it possible? Thank you.
Asked
Active
Viewed 65 times
-1

Niel Sinel
- 291
- 2
- 6
- 15
-
You can see this already given answer on http://stackoverflow.com/questions/1034621/get-current-url-in-web-browser that will help you. – Muhammad Usman Sep 01 '15 at 05:08
-
[Here's](http://phpjs.org/functions/basename/) a JS implementation of PHP's basename function. It'll get what you need. – Darren Sep 01 '15 at 05:09
-
possible duplicate of [Get current URL in JavaScript?](http://stackoverflow.com/questions/406192/get-current-url-in-javascript) – Marcel Burkhard Sep 01 '15 at 05:15
3 Answers
2
You can use location object to do this, To remove the "/" before the file name you can use substring method.
location.pathname.substring(location.pathname.lastIndexOf("/") + 1);

Vibhesh Kaul
- 2,593
- 1
- 21
- 38
2
you can try this pretty simple
var url = 'localhost/empc/myfolder/functions.php';
var url = url.split('/');
alert(url.pop());

Darren
- 13,050
- 4
- 41
- 79

Bagwala Irfan
- 59
- 5
1
It is possible. Use the javascript split function to separate the string into pieces and then access the last element to get the file name:
var str = "localhost/empc/myfolder/functions.php";
var arr = str.split("/");
alert(arr[arr.length-1]); //this will alert functions.php

DannyPhantom
- 1,042
- 10
- 21