JavaScript get available files in path - javascript

Is there a way to get all of the available files in a certain path with javascript?
An example would be that the user would type a place of a folder into an <input> like "C:\Program Files" and JavaScript would get some array of all the available folders in their Program Files folder.

No, JS doesn't have access to the file system / hierarchy. Server side JS is a different story but I think you don't mean to ask about it.

Related

Why do some .js files appear plain text files on vscode?

Like in the image below, while most .js files turn out to be javascript files, some in the same project choose to be text files although they still have .js on the file name. The content is also in correct javascript syntax and I can't tell what went wrong.
Does anyone have an idea how to fix this?
The most common explanation is they are .txt files. In other words, it means that the file's full name is something like index.js.txt.
Can you check the file type? You can quickly do that by:
Right-click on the file name;
Select the option "copy path";
Past it in some .txt file. Check if you obtain something like this: C:\{some-path}\index.js.txt
If you still see index.js check the file type. Sometimes when new files are created, the operating system does not create that file with the extension type that we define in the file name.
The solution is just to create another file, ensure that the operating system detects the file type, copy the content to this new file, and delete the old one.

HTML src not correctly accessing required filepath

I have included a folder where I keep files I don't want people easily looking at, like passwords in php connection files. This folder is called 'inc' and is at the same level as the 'public_html' folder. I have put php files with database connection details in the 'inc' folder and accessed this using '../inc/' and the filename and it works perfectly - i.e.:
require_once('../inc/connection.php');
I would also like to put javascript files in the 'inc' directory and access them via the src path in HTML. I have tried the same approach as with the php file - that is using '../inc/' and the file name to access the files and for some reason it is not working:
<script src="../inc/moment.js"></script>
I have read similar queries to this on Stack Overflow and followed the right approach (locally and on server) but for whatever reason it is not accessing the js file. It does work however if I relocate the 'inc' folder to within the 'public_html' folder (for js files only) but this defeats the purpose of what I am trying to achieve - and in my mind should not work.
Does php treat the access of directories different to HTML's src hence different behaviour using '../'. I don't think it should and yet I can't get it to work.
Any help greatly appreciated!
A script tag with a source attribute is html that tells the browser to go get a file. Relative paths can be used, but your js files will need to be accessible to the public; they simply can't be hidden outside of the public root.
PHP is a scripting language running on your server. When it sees ../ in the proper contexts, such as the require_once directive you mention, it translates that into the proper path to the directory you are looking for, so it can look outside of the public directory.
Don't put sensitive items in the js, put them in protected files on the server. If you need the info at runtime, make an ajax call to access and use the data to provide the client with only the information it needs.

How to use Javascript to read file in Rails tmp?

The way I have it right now is that I use JavaScript to read a file, providing a hard coded path. I tell it to look it /public. I'm going to generate a file in the tmp directory with Rails, and I want to read it with JavaScript. How can I do this? What is the tmp directory of Rails?
I've tried putting the file into /tmp and hard coding JavaScript to read from /tmp, but it doesn't load the file.
I might be wrong but I think browser can access only files inside public folder unless it goes through Rails route. So, you can either change the location of tmp folder or you can create a method in a controller that will read that JS file and send it back to browser (sort of like proxy).

Access filesystem using js within grails project

For learning purposes I created a toy project using grails.
In this project I would like to play an mp3 file in one of my views.
I found an html5/flash mp3 player. But when the music is supposed to play, I have a question.
The HTML 5 player is in the project/web-app/js folder. Everything else is in its normal place.
When I try to access a file, let's say "test.mp3" using just the name and extension of the file, where this file is supposed to be?
The file should be relative to whatever your current path is.
That is, if you're at
localhost:8080/myapp/test/test1, and you reference test.mp3, test.mp3 would have to be in the test1 directory. You can use absolute paths, such as /sounds/test.mp3 to reference from the root, but a better solution would be to use ${resource(...)} probably.
Here's the resource documentation.

How to access proprietary .js file in Spring MVC?

I am newby in Spring, but have a task, and I am learning on the fly.
I used Roo to generates for me part of the code, but now I have to make some dynamic list binding, which is done with form, popping-up in new window, and when the submit button is pushed I have to insert the new values in the parent window.
For the purpose I wrote a .js file, which hooks the values to the parent DOM tree, but the point is that I can't configure Spring to deliver the required .js file to the browser.
The browser, doesn't recognize my function. Even when I try to access the .js file via the browser, I receive error that the file couldn't not be found.
I've tried to configure the web.xml, but it didn't work...
Any ideas, how I can configure the access to a .js file in a Spring MVC application?
Thanks a lot!
P.S. Respectively, I'll need to grant access for a static .htm(l) file... I suppose the principle for configuration of the access of static html files is the same..., right?
You just need to get the path to the file right. Assuming you have a Maven-like set-up (I assume you do because you're using Roo), then your script belongs under src/main/webapp - probably in something like a scripts folder.
Let's assume that your file is at src/main/webapp/scripts/myscript.js
You can create a URL reference for your script by adding the following Spring tag:
<spring:url value="/scripts/myscript.js" var="script_url"/>
This should give you the right path to your script, regardless of the context in which you later decide to publish your webapp.
After that, it's just a matter of using that reference:
<script type="text/javascript" src="${script_url}"></script>

Categories