简体   繁体   中英

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 (ie /myapp/ ) so you can concatenate /myapp/ to js/some.js and get the right URI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM