开发者

JS Include is not working

开发者 https://www.devze.com 2023-03-11 15:52 出处:网络
I have moved an large piece of JS code form my header file to it\'s own .js file. I\'m trying to include it with:

I have moved an large piece of JS code form my header file to it's own .js file. I'm trying to include it with:

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

The JS开发者_如何学编程 code is not loaded, what could be wrong?


One possible reason is that the path is wrong. Remember that the path as you've written it will be interpreted relative to the current URL. So if this code appears on a page that is accessed at

http://www.example.com/example1/index.html

then the browser will request the javascript file from

http://www.example.com/example1/includes/js/test.js

which may not be what you want. A better approach may be to use a link that is based on the root: that is, if you change it to

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

then it will always look in

http://www.example.com/includes/js/test.js

no matter where the link appears.


If I had to guess based on the information you've provided, I'd say there's probably a syntax error in the external .js file or the page isn't finding the file from the URL provided. That's usually the problem I experience when I move large pieces of code from one file to another.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号