简体   繁体   English

如何引用父文件夹中的javascript?

[英]How to reference javascript which is in the parent folder?

I have a web applictaion which use has the following folder structure 我有一个使用以下文件夹结构的Web应用程序

application_root js application_root js

in the html, I refer the js like 在html中,我将js称为

<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. 如果我使用file:/// protocol来启动html页面,那么一切都是文件,但是当我使用Web服务器时,例如http:// loclahost:6000 / application_root ,我发现js无法正确加载。

How to solve this issue? 如何解决这个问题?

You need to start your path with / : <script src="/js/some.js"></script> 您需要使用/开始路径: <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. 例如: /js/some.jshttp://localhost/js/some.js ,如果您的网站托管在虚拟目录(例如http://localhost/myapp/js/some.js此方法会成功不行

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. 如果您发现上述问题的一部分,则可能需要使用服务器端代码来获取应用程序的根目录(即/myapp/ ),以便可以将/myapp/连接到js/some.js并获取正确的URI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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