admin管理员组文章数量:1316323
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?
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?
Share Improve this question asked Mar 28, 2019 at 17:10 prkmkprkmk 3631 gold badge4 silver badges11 bronze badges 3-
When you're looking at
index.php
, what does the address bar show the URL is? – T.J. Crowder Commented Mar 28, 2019 at 17:13 - 3 Click F12, network and look at the actual path it tried and failed – mplungjan Commented Mar 28, 2019 at 17:13
-
1
Side question; What does
(for safety reasons)
entail that keeping the js file in the same folder as the html would make less safe? – Taplar Commented Mar 28, 2019 at 17:16
2 Answers
Reset to default 7PHP resolves paths on the puter'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
本文标签: Javascript in different folder than HTMLStack Overflow
版权声明:本文标题:Javascript in different folder than HTML - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742003859a2411562.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论