1

Can I get the current .php or .html file name by JavaScript? The script is in the external file in respect to current .php or .html, so need the name file name of this current .html of .php:

<!DOCTYPE html>
<html>

<head>
    ...
    <script src="js/Script.js" type="text/javascript"></script>
</head>

<body>
    ...
</body>
</html>

I tried this but the variable value was empty, maybe because currently I use the local host. Anything that can to do which will be work on both local and remote host?

4
  • Depends on the way you are running the server. window.location may help but depends on your routing algo. for. eg if you run in this page you will actually not get php or html because the uri routing will not show it Commented Jul 13, 2016 at 3:29
  • 1
    Possible duplicate of Getting the current script executing filename in javascript Commented Jul 13, 2016 at 3:36
  • Pls say why soln of stackoverflow.com/questions/2196606/… doesnt work as it uses same window.location.pathname as ur accepted answer. Commented Jul 13, 2016 at 3:40
  • Sorry, my mistake. Really I used the ` var fileName = location.href.split("/").slice(-1);` solution because it is short. I could not delete this question myself and make the flag. Sorry again. Commented Jul 13, 2016 at 5:41

3 Answers 3

3

Below is the simple code to get full URL and filename of URL:

Example1:

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

OUTPUT1:

url => http://localhost/demo/index.php
urlFilename => index.php

OUTPUT2:

url => http://localhost/demo/
urlFilename => 

urlFilename will be empty in case of url is rewritten to (http://localhost/demo/)

Example2: I do not know if you want to use php inside javascript, if you are desperate to get the filename this is one way to do it.

var filename = '<?= __FILE__?>';alert(filename);
//output: ex: /opt/lampp/htdocs/demo/index.php
var url2 = filename.substr(filename.lastIndexOf('/')+1);
//output: ex: index.php
Sign up to request clarification or add additional context in comments.

Comments

1

Window Location

Verify if this document can help you.

It must be something, like this:

var page = window.location.href;

4 Comments

Only in a rudimentary implementation. If you use a templater engine or routing in php window.location will not see anything releavant. for example this page will give /questions/38341122/how-to-get-current-php-or-html-file-name-by-javascript. Not the html or servre filename
Yes, you're right, this code is good in case URL have the script name.
he has said stackoverflow.com/questions/2196606/… doesnt work. how is that question different?
This post that you show me is an alternative for the problem of Gurebu
0

Create following function to get the current .php or .html file's name in JavaScript.

const getCurrentFile = () => {
    let url = window.location.href;
    let objs = url.split("/");
    return (objs[objs.length-1]);
}

Here, with the help of JavaScript's split function, we splits the url with "/" and then returned the last element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.