HTML src not correctly accessing required filepath - javascript

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.

Related

JavaScript get available files in path

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.

How to reference javascript which is in the parent folder?

I have a web applictaion which use has the following folder structure
application_root
js
in the html, I refer the js like
<script src="../js/****"></script>
everything is file if I start the html page using file:///protocol, but when I use the web server, like http://loclahost:6000/application_root, I found the js cannot be loaded correctly.
How to solve this issue?
You need to start your path with /: <script src="/js/some.js"></script>
Anyway, this can be problematic because if you use a virtual directory, / won't work since it's the root path.
For example: /js/some.js is http://localhost/js/some.js, and if your web site is hosted in a virtual directory like http://localhost/myapp/js/some.js this approach won't work.
If you find above case part of your issue, you might need to use server-side code to get your application root (i.e. /myapp/) so you can concatenate /myapp/ to js/some.js and get the right URI.

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).

Django URL Config for javascript files

Hey guys - I am having trouble setting up my Django urls file correctly for the following thing!
<script type="text/javascript" src="/javascript/HashMap.js"></script>
What happens is the file is attempted to get found : http://localhost/javascript/HashMap.js but this URL does not match any within the URL Config and therefore the GET request fails.
Can anyone lend a hand to help me find the correct line to add to allow this kind of thing to work!
Cheers
Andy
You need to serve your JavaScript as a static file.
How you do this in production depends on what Web server you're running with. Django doesn't normally serve up static files.
For example, for Apache, you'd put this in your Apache config:
Alias /javascript/ /usr/local/wsgi/static/javascript/
Then you can put your .js files in the directory referenced.
The Django docs have a entire page on just this topic (serving static files): http://docs.djangoproject.com/en/dev/howto/static-files/
Javascript is usually considered a static resource file. If that's true in your case, I would refer you to the Django documentation. If you really need to use urlconfs to point to a view that generates Javascript, then you will need to make an entry in your URLconf for it and point it to a view.
If you're trying to get only the file as static content, the problem will be surely at your MEDIA_ROOT and MEDIA_URL settings variables. Just check they're pointing to where the javascript file is and your MEDIA_URL should have a postfix to ensure that you're accessing an "special" directory where the media files are stored.
In the case you're trying to do some processing over the file, generating it at runtime, etc.
try with this regular expression:
r'^javascript/(?P<jsfile>.+\.js)$'
In your view declaration, instead of only get the request as parameter, you will have the request and a new param called jsfile, that is the filename you'requesting. So in the view, do the processing and return an HttpResponse object with the processed file contents.

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