I'm new to web development. I have issues with having files stored in different folders (for safety reasons). Here's the index page which returns an error
<head>
<script src="../scripts/script.js"></script>
</head>
<body>
<?php include "../php/page.php"; ?>
Other code here...
</body>
My file structure is as follows
www/
html/
index.php
scripts/
script.js
php/
page.php
I don't get why including php file works (row 5 in the example code provided) and including javascript doesn't (row 2). I guess you're interested about the error so here's what Google Chrome's console says
Failed to load resource: the server responded with a status of 404 (Not Found)
It also shows that link to the resource and it appears to look for my.server.address/scripts/script.js like it doesn't care the ../ part. Can someone explain this behaviour?
PHP resolves paths on the computer's file system.
Web browsers resolve paths on the URL.
Your HTTP server has http://my.server.address/ mapping to www/html on the file system.
So ../scripts/script.js goes up one level from / … to / (because that is the top), then down to /scripts then down to /scripts/script.js.
The browser asks the HTTP server for /scripts/script.js and it maps that to the file system — www/html/scripts/script.js and returns a 404 because that file doesn't exist there.
The browser can only read data that the web server will send to it, and by keeping the scripts directory outside of the document root you haven't make it available via the web server.
Change
<script src="../scripts/script.js"></script>
to
<script src="/scripts/script.js"></script>
And your folder structure should be:
www/html/index.php
www/html/scripts/script.js
www/html/php/page.php
Related
I am hosting a web server using Express JS, and have middleware set up so that all requests to *.mydomain.com/cdn/* get forwarded to cdn.mydomain.com. In my html file I have the following lines:
<style>
#import url("/cdn/style.css");
</style>
<script src="/cdn/script.js"></script>
However, when looking at Developer Tools > Sources on the site, it shows the css file as cdn.mydomain.com/style.css but the javascript file as mydomain.com/cdn/script.js.
When looking at the network tab, the request gets properly forwarded then loaded, with the final Request URL header showing https://cdn.mydomain.com/script.js.
Is this performance expected of javascript files or is something playing up? Thanks in advance for any advice you're able to offer.
I have a problem with loading a JavaScript file on a jsp page when I deploy my web application on a weblogic server. Before I deployed it on Tomcat 7 and it worked normally.
First I see on console window of Firefox. My jsp page couldn't load js file on /resources/ folder (this folder is the same level with /WEB-INF/):
Loading failed for the <script> with source “http ://10.3.11.25:7001/resources/assets/global/plugins/jquery.min.js”. 10.3.11.25:7001:104
Image I have capture:
I tried copying the url: http ://10.3.11.25:7001/resources/assets/global/plugins/jquery.min.js to the address bar.
I can access it, but I only can download the js file (It not display the source code on browser as normally).
What is my problem? I deploy my web application on weblogic 12c.
UPDATE:
Network tab load js file ok, all status is 200:
Source code include on jsp:
<script src="resources/assets/global/plugins/jquery.min.js"
type="text/javascript"></script>
<script src="resources/assets/global/plugins/jquery-migrate.min.js"
type="text/javascript"></script>
UPDATE 2:
All status is 200 but load O KB and response is notthing
When I copy the js url to address bar it show popup download it (not display the source code as normally)
I have resolve my problem. My web application can't read the js file beacause have problem with MIME type on weblogic.
I add following mime mapping to web.xml and problem has been resolved:
<mime-mapping>
<extension>xml</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>js</extension>
<mime-type>text/javascript</mime-type>
</mime-mapping>
I feel like asking the most stupid question, but i searched for a while now and could not get it figured out.
I have the following file structure under my Apache DocumentRoot:
DocRoot
- page1
- ...
- webapp
- index.html
- somescript.js
with index.html having a script tag looking like
<script src="somescript.js" type="text/javascript">
How do i configure Apache to serve https://myhostname.com/webapp so that the script get's loaded correctly? Page1 should stay accessible under https://myhostname/page1.
The current behaviour is that somescript.js does not get found, because the request is https://myhostname.com/somescript.js.
I do NOT want to set up a Virtual Host for this or edit the html file (get's generated).
Are webapp and page1 located in var/www/html? If so, probably your JS file is searched in html folder.
You have, at least, 2 solutions:
<script src="/webapp/somescript.js" type="text/javascript">
Configure virtual hosts for webapp and page1. So, you'll be able to connect to that pages via webapp.myhostname.com and page1.myhostname.com subdomens, and <script src="somescript.js" type="text/javascript"> will work for both of them, searching in their folders
I wrote a simple 404 page for my website that tells you a joke every time it is loaded. I am accomplishing this with the use of the StackExhange API and am drawing questions off of this thread.
Everything works as intended when I purposely trigger the 404 page with a garbage file url. For example when I go to www.domain.com/garbage, the 404 page loads as expected. My 404.shtml has a script tag that loads a 404.js script that I wrote and everything is great.
However, when I attempt to get a 404 page by routing to a garbage directory as apposed to a garbage file, the script cannot be found. For example, www.domain.com/garbage/ does NOT load the 404.js script despite the correct 404.shtml page being loaded.
Here is the head section of my HTML file.
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script src="404.js" type="text/javascript"></script>
</head>
Note: The external jQuery script is loaded both times, but 404.js is only loaded for the first request example.
Here is the console for both requests, the first one is the request for a garbage file and the second is the request for a garbage directory.
First URL: www.domain.com/garbage
// a long JSON response from the StackExhange API that indicates that the script loaded
Second URL: www.domain.com/garbage/
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (404.js, line 0)
Question What is causing the 404.js script to not be loaded when I attempt to access an invalid directory within my site when the same script loads when I attempt to access an invalid file?
What I Have Tried
A plethora of Google searches.
Triple check that all script tags are pointing to the correct files
Thank you for your time. If you wish to do some more digging, you can see the actual problem in action at briantracy.xyz/garbage and briantracy.xyz/garbage/.
Big Thanks to #rfornal for suggesting making a relative file path to the script. Channging the following line in the head section
<script src="404.js" type="text/javascript"></script>
^^^^^^
by prepending the root directory (where 404.js is located) to the file path does the trick.
<script src="/404.js" type="text/javascript"></script>
^^^^^^^
On my site I have my resources folder outside of the root, for example:
/var/www/html/ is the root directory
/var/www/resources/
I currently have a config file that sets the location of the library so I can include it with php like so:
defined("LIBRARY_PATH")
or
define("LIBRARY_PATH", realpath(dirname(__FILE__) . '/library'));
which works perfectly when I use:
<?php include_once(LIBRARY_PATH . "/file.php"); ?>
but it doesn't work when trying to add Javascript files:
e.g.
<script src="../resources/library/js/test.js"></script>
links to 'www.website.com/resources/library/js/common.js'
or
<script src="<?php echo LIBRARY_PATH; ?>/js/test.js"></script>
links to 'www.website.com/var/www/resources/library/js/test.js'
neither of which work.
Any suggestions on how I can do this without having the js files in or above the root?
Your JavaScript files have to be accessible to the browser because they are executed by the browser and not by the server.
This requires that they have a URL.
Putting the files under the webroot is the standard way to give a static file a URL.
Alternatively, you could write a program (e.g. in PHP) that will read the file and then output it's content to the browser. This is more complicated and makes dealing with cache control headers more fiddly and is not recommended.
Assuming you understand what you're doing and security implications of that!..
You create the linkjs.php script that takes the relative path to the script (from some root dir, perhaps /var/www/resource/js) as a parameter, like:
<script src="/linkjs.php?p=test.js">
In your PHP script you resolve the full file path, check that it's indeed a file under the root dir (to protect against ../ in the parameter), that it's readable by you PHP user, read the content and output it into the response. Don't forget to set content type to text/javascript of course.
Ideally, you should also provide proper caching headers based on the source file modification time, but that is a topic in itself. See the guidelines in other SO questions about proper caching headers for dynamic content.
The upside is that you can do on-the-fly script minification/combining/wrapping/substitutions if you like/need.