最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Trouble including .js file into php - Stack Overflow

programmeradmin0浏览0评论

I am trying to include a .js file into a php file. My folder structure looks like this:

root
---js (FOLDER)
------file.js
---blog (FOLDER)
------index.php
------js (FOLDER)
---------blog.js

If I am using this:

<script type="text/javascript" src="../js/blog.js"></script>

it works just fine.

What I can't seem to do is include file.js from under the root's "js" directory. I've tried everything I can think of but I just can't seem to make it work.

I am trying to include a .js file into a php file. My folder structure looks like this:

root
---js (FOLDER)
------file.js
---blog (FOLDER)
------index.php
------js (FOLDER)
---------blog.js

If I am using this:

<script type="text/javascript" src="../js/blog.js"></script>

it works just fine.

What I can't seem to do is include file.js from under the root's "js" directory. I've tried everything I can think of but I just can't seem to make it work.

Share asked Aug 29, 2009 at 4:59 James P. WrightJames P. Wright 9,12923 gold badges82 silver badges144 bronze badges 1
  • Well this is actually on my localhost right now for testing purposes, which means that the file is actually located at /localhost/php/mysite/js/ I was hoping for a way to be able to just go up two levels and start from there. Is there no way to do that at all? – James P. Wright Commented Sep 1, 2009 at 23:36
Add a ment  | 

3 Answers 3

Reset to default 4
<script type="text/javascript" src="/js/file.js"></script>

would reference the javascript file in the top level js directory

To access the blog, is a user going to www.domain. or www.domain./blog?

If /root/blog is your DOCROOT in your web server, the /root/js directly likely isn't web-accessible. The location you're referencing MUST be accessible to the client's browser.

An easy way to test this is to enter the path to the JS file directly into your browser.

it looks like blog is in a subdirectory of the folder index.php is in. Discard the ../ bit.

<script type="text/javascript" src="js/blog.js"></script>
<script type="text/javascript" src="../js/file.js"></script>
发布评论

评论列表(0)

  1. 暂无评论