Javascript 如何解决include文件中的相对路径问题?

Javascript 如何解决include文件中的相对路径问题?,javascript,html,Javascript,Html,我的应用程序结构如下所示: /Views/Page1.html /Views/SubFolder/Page2.html <script type="text/javascript" src="../script/commonIncludes.js"></script> 现在每个页面的头部都有一个指向javascript的链接,其中包含如下常见内容: /Views/Page1.html /Views/SubFolder/Page2.html <script ty

我的应用程序结构如下所示:

/Views/Page1.html
/Views/SubFolder/Page2.html
<script type="text/javascript" src="../script/commonIncludes.js"></script>
现在每个页面的头部都有一个指向javascript的链接,其中包含如下常见内容:

/Views/Page1.html
/Views/SubFolder/Page2.html
<script type="text/javascript" src="../script/commonIncludes.js"></script>

到目前为止还不错,因为我们知道每个起始html页面的位置,并且可以直接调整脚本目录的相对路径

问题在于,在这个commonIncludes.js文件中,有一些代码实际上会写出js includes,如下所示:

// JQuery UI 
document.write('<script type="text/javascript" src="../script/jquery-ui-latest.min.js"></script>');
document.write('<script type="text/javascript" src="../script/jquery-ui-layout-latest.min.js"></script>');
//jqueryui
文件。写(“”);
文件。写(“”);
这就是问题所在,因为相对路径不是从commonIncludes.js计算出来的,而是从calling.html页面计算出来的,它可以驻留在应用程序结构的任何地方

从根目录引用也不会像下面的“\”那样工作,因为该站点使用IIS应用程序,因此URL最终看起来像这样:

/Views/Page1.html
/Views/SubFolder/Page2.html
<script type="text/javascript" src="../script/commonIncludes.js"></script>


因此,我正在寻找一种方法来调整或设置路径,而不管调用的html使用相对路径位于何处

您是否尝试将当前URL与“窗口”对象一起使用?然后,您可以使用window.location.host了解您在witch环境中的身份

而不是

document.write('<script type="text/javascript" src="../script/jquery-ui-latest.min.js"></script>');
document.write(“”);
用这个

document.write('<script type="text/javascript" src="http://host/script/jquery-ui-latest.min.js"></script>');
document.write(“”);
//使用此样式

document.write('<script type="text/javascript" src="//script/jquery-ui-latest.min.js"></script>');
document.write(“”);
This src=“../表示您在使用“~/foldername”、“~foldername”项目目录之前取出了两个文件夹。只是想提供帮助。您可以仔细检查文件位置,并将其与代码进行比较