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
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.
On a Local Machine it is not possible, however you could use:
document.location.href;
to get the URL, where your file is hosted
no you can't browser don't allow javascript to access the file paths due to security reasons
Related
I'm new to javascript might be this question looks silly. How to check whether the html files are loading from local file system or server using javascript?
For example when I open the html files from my local system (/home/user/1.html) browser shows in url
file:///home/user/1.html
But if i load 1.html file into my local server then, if i access that file browser shows in url like below
http://localhost/GUI/1.html
I want to find whether files are loading from my server or local file system using java script.
Is there any way to find this using java script method.
You can use the window.location object to do that.
use window.location.protocol property.
The following line of code will return true if the file is being served from the file system.
window.location.protocol == "file:";
It basically checks if the protocol being used is file or not.
If I have an HTML page with a script tag like this:
<SCRIPT SRC="./xxx.js"></SCRIPT>
Under what conditions would that ./xxx.js be gotten/accessed from the local filesystem?
I understand that the ./xxx.js URI/URL references the "the file named 'xxx.js' in the current directory", but when (under what conditions) will "current directory" mean the current directory on the local filesystem on which the client/browser is running?
Is the only situation where that would be the case be when the HTML file containing that <script> tag was retrieved from the local filesystem?
I understand that the "./xxx.js" URI/URL references the "the file named 'xxx.js' in the current directory",
More correctly, it means it will reference the file named xxx.js relative to the current file.
That means it will look in the same directory that the file containing the <script> tag was loaded from. If it's the local file system, it will load it from there. If it was served from a webserver, it will issue a new request to the webserver for that file.
The protocol of the URI determines how files are requested. In your case the path is relative, so the protocol used when requesting the html page will be used. Let's say the html file is index.html if you then request it like this http://localhost/index.html. The your script file won't be server from the local file system, whereas if you request it like so file://path/index.html your script file will be served from the local file system
Oky, so..
The src attribute will look for the file in the path given to it is always be relative, when you load the web page locally it will look for that file in the given path, no matter if the path is local to you or if its local in a web server.
the path has to be correct
and remember if you put the javascript in the script tag along the src the javascript that you put on the script won't work.
When src value is a path, it will be considered local and may be a relative path or absolute path:
relative path examples
folder/subfolder/file.jpg
../parent_folder/file.jpg
absolute path example
/root/folder/subfolder/file.jpg
When it is remote, src's value will be a URL like http://yourappsite.com/route/file.jpg
My requirement is to read the json file which contains some data and store in some other .js file.
I got task to read local file from local disk in Javascript , i have used file path like - D:\json\analytics.json.
(document).ready(){
($).getData("D:\json\analytics.json");
}
when i see in firebug it takes other url.
How I can do it, is it possible to read file from javascript.
I don't know javascript , i have seen some answer but i am not able to understand .
Need Solution , how I can achieve it. is there any other way to read file on jsp without using scriptlet . From server side , can send it on the jsp page.
I think Jaronmanda's answer won't work cause it will hit cross origin issue, see "Cross origin requests are only supported for HTTP." error when loading a local file.
As the page suggested, in general you need to serve that json file from a web service (same domain, or allow your domain to access), but it depends on what you really need to do. If you can control where that json file is stored, the easier way is to put that in a subdirectory of your html file, and do:
$(document).ready(function () {
$.get('<directory>/analytics.json', function (data) {
// Do your stuff
});
});
I have the following code:
<script>
var fileContent;
$.ajax({
url : "text.txt",
dataType: "text",
...
It loads the text from a .txt file in order to obtain the data. If the text.txt is on the same path as the html code, it loads the data. However, if I type for example (placing the file in a different folder):
url: "../../../files/text.txt"
It does not allow me to obtain the file. Any ideas of how to do it or how to implement it without changing the code in a significant way? Thanks!
There are three possible causes of this:
You are using HTTP
You are using HTTP and the path you are trying to access is not exposed by your web server. (You cannot access files above the directory root by default).
You need to give the file a URL on your web server and request that URL.
You are using local files
Different browsers have different security restrictions for Ajax on local files.
You can get this problem if the file is in a directory above the HTML document (some browsers only allow you to access files in the same or a lower directory).
You can resolve this by using HTTP. Ajax in general works very poorly without HTTP.
You simply have the URL wrong
Correct the URL.
On a Linux server, I am trying to access files in a separate folder on the same server, but can't quite seem to reach them. The PHP script is located in a subdomain folder:
/home/sofia/public_html/mail.domain1.com/index.php
and I am trying to load an image resource from a different folder, with absolute paths like these (JavaScript DOM):
document.body.style.backgroundImage = "url('/home/sofia/public_html/common-res/bgImage.jpg')";
document.body.style.backgroundImage = "url('/home/sofia/common-res/bgImage.jpg')";
or even with relative paths:
document.body.style.backgroundImage = "url('../common-res/bgImage.jpg')";
document.body.style.backgroundImage = "url('../../common-res/bgImage.jpg')";
Is there something wrong with the syntax, or are such references simply not allowed?
Thank you for your kind attention.
Local paths such as /home/sofia/common-res/bgImage.jpg will not work. You must either use full or relative URLs:
document.body.style.backgroundImage = "url('../../common-res/bgImage.jpg')";
document.body.style.backgroundImage = "url('http://example.com/common-res/bgImage.jpg')";
Both are acceptable, just make sure they resolve to a directory that is properly being served.
Just an update for the benefit of others. Support from my hosting company has informed me that for such servers where multiple domains reside on a single account, each domain or sub-domain in the public_html folder is treated as a separate domain, and thus cannot breach the cross-domain protocol.
However, since they are on the same server, a simple solution would be to create symlinks within each domain or sub-domain, to enable them to access local resources beyond their scope.
In any case, the solution offered by Yuval Adam and Prava works just as well. Thank you both.
It should also be noted, that depending on the host server configuration, referencing a http path would incur connection overheads, while relative references do not.