I have a html file under my war folder in the project ,
I just want to access that file From the path "/myhtml.html"
and then replace whatever is in this file from a new text "Some new Text"
so that My html file now will only have "some new text" there .
I searched alot for this , but they provide example for changing a specific div etc in a file , but i just want to replace everything in a file which is present under a war folder.
I understand that you are trying to change the content of HTML files using Javascript that is running in the browser.
Front-end Javascript can't access local files that are stored on your machine. It would be a security disaster if it could. The only way Javascript can read with local files is using a file upload input, and it will only be able to read the files not update/overwrite them.
NodeJS however is capable of accessing the local file system. NodeJs is backend JavaScript so it will be running on a server.
Related
I have a project where I visualise data from a file (say, a CSV). My visualiser is a .html bundle created with Webpack. Up until now, I include a placeholder for the data as a string in the bundle. If I want to change the data, I replace the placeholder in the bundle .html file with the new data I want it to show (via an external script).
Now I'd really love to change this for convenience. For example, I'd like to get a list of all the supported files that are in the same directory as the .html file and load their data, either in the background or when I click or them or whatever. (In the easiest use-case, there would just be 1 data file in the directory and the project would just load this data automagically at startup.)
I had a look at Handlebars.js and I think it might help with the injection of the data. However, it seems that there is no possibility to read from the directory I start the .html file from. I already tried fs and path but those don't work in the browser when I just want do double-click the .html file.
Is there a possibility for the browser to read files from the directory I open a .html file from? And if so, how can this be done in Javascript/Typescript?
I have an HTML file with JavaScript that I am running without any Webserver/host so I am just opening the file in a browser local to my windows PC. In that HTML file I would like to be able to read a text file in the same folder as the html file. That file will contain data in rows and columns separated with tabs. i.e
1 a
2 b
3 c
I want to keep this as simple as possible so all I have to do is share the HTML and Text file to others so the can open it up local to their computer without any webserver/host and without having to also copy of external libraries like node.js or jquery.
I have searched and tested everything I can find but either I need to reference an external library or I have to run it in a webserver or I need to click a button to load the file through the browser, none of what I want.
Does native JavaScript support the function to read a text file and save it to an array? If so, any code direction would be great.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
XMLHttpRequest() exists in native JavaScript, I think it will help you.
You also can send a request to the file. Or use library: axios.js because when you use XMLHttpRequest() you lose many time to write code which just get content from file, with axios I got file content with one line: `axios.get('file.txt').then(result => console.log(result.data));
To connect Axios: <script src="https://unpkg.com/axios#0.18.0/dist/axios.min.js"></script>
You can read official documentation about axios.js and XMLHttpRequest() in the net.
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.
I am creating browser based video editing tool. I want a user to first download a ~70mb javascript file and store it somewhere on his computer. I want to link that file when my website is opened. How can I achieve that.
EDIT
What i meant is that there are various files like js1.js,js2.js... all sums upto 70mb . So i will offer a zip folder to download and only link js1 or js2 file etc depending on the effects user wish to apply
i am sorry to inform you but i think there is something really wrong with what you are trying to do.
A "solution" would be to just cache the javascript on the user's browser so any subsequent requests parse the cache instead of requesting the resource again from the server.
You should know however that if you are in need to download ~70mb of a javascript file you are doing something wrong. I have a whole web app project that when published the total size is around 60mb, all files required to properly run included, and its a damn big codebase in there.
I find it very hard to believe there is ever a need for a single javascript file to be that big, in any case maybe a simple caching should do the trick
That is actually done automatically. Once you add a <script> tag with a link to a local js file (also stored on the server) the file is loaded automatically.
See HTML <script> src Attribute for more information on that.
You can only reference to js files on the server. Files on the server could look like this:
index.html
somefancyjsfile.js
You can then reference from inside your html file to the js file via the <script> tag.
I'm not sure though if the size is not a bit too much...
Is their a way- client side,
to add text to a file named text.txt in javascript?
In python:
f = open("text.txt","w")
f.write("Hello World")
f.close()
would write "Hello World" into the text file. I want to do something similar with javascript.
Note: I am running these files locally.
Its a bit more tricky than in python.
The problem is that the browser sandboxes your session and your possibilities.
It is possible to request storage space and read and write files in that space.
More info: http://www.html5rocks.com/en/tutorials/file/filesystem/
It would be a security nightmare if a javascipt file could just open files in the root or any other folder.
A different approach would be to upload the file in the client/ to the server edit it and send it back in a response.