Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current file path in javascript

can i get the full path of my html page through javascript eg My Index page is at

         d:/somefolder/anotherfolder/index.html

so can i get this path

Thanks

like image 273
shoaib Avatar asked Sep 04 '13 11:09

shoaib


People also ask

How do I get the current directory in JavaScript?

In Node. js, there are two built-in ways to get the current directory. You can either use the __dirname variable or the process. cwd() method to get the current folder.

How do I get the current path in node JS?

We can get the path of the present script in node. js by using __dirname and __filename module scope variables. __dirname: It returns the directory name of the current module in which the current script is located.

How do I get the current directory in HTML?

The proper method would be ./filename. ext . ../ addresses the path up one level from the current folder. If you were in the path /cheese/crackers/yummy.


2 Answers

You can only access your current file path relative to your host URL, you cannot access the folder path of that file.

This is restricted in JavaScript due to security reasons. If that was allowed, malicious scripts could easily read your server's internal folder structures, which is bad.

like image 66
MD Sayem Ahmed Avatar answered Oct 22 '22 19:10

MD Sayem Ahmed


On a Local Machine it is not possible, however you could use:

document.location.href;

to get the URL, where your file is hosted

like image 31
defau1t Avatar answered Oct 22 '22 18:10

defau1t