【问题标题】:javascript file not loadingjavascript文件未加载
【发布时间】:2013-03-01 02:08:03
【问题描述】:

试图从我的页面导入我的 js 文件。

我的页面在webcontent/mydomain/templates/page.xhtml

我的js在webcontent/mydomain/test/scripts

在page.xhtml中

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

但脚本仍然没有被选中。

谁能告诉我需要如何在 src 中给出路径。

【问题讨论】:

  • 尝试完整路径&lt;script src="c:/....mydomain/test/scripts/test.js"&gt;&lt;/script&gt;
  • 试试:&lt;script type="text/javascript" src="./test/scripts/test.js"&gt;&lt;/script&gt;
  • 你真的需要展示你的代码。

标签: javascript jsf-2


【解决方案1】:

试试这个:

<script src="/test/scripts/test.js"></script>

【讨论】:

  • 您确定从服务器打开页面,而不是文件?
  • 我使用的是JSF2.0,所以只有xhtml。我也不能使用 h:outputScript 因为我不能将 js 放在资源文件夹中。下面的代码是否仍然有效“...我使用的是 JSF2.0,所以只有 xhtml。我也不能像我一样使用 h:outputScript不把js放在资源文件夹中。下面的代码是否仍然有效"
【解决方案2】:

假设webcontent 是公共Web 内容的根,因此/mydomain 也是一个公共文件夹,因此您的JavaScript 可以由http://localhost:8080/context/mydomain/test/scripts/test.js 独立使用,假设域为http://localhost:8080,上下文路径为@ 987654325@,那么应该这样做:

<script src="#{request.contextPath}/mydomain/test/scripts/test.js"></script>

这将生成一个具有动态内联上下文路径的域相对 URL,这比摆弄 ../ 更加健壮,这将使 URI 相对于当前请求 URI(如您在浏览器的地址栏中看到的那样)和不要像许多初学者错误地假设的那样指向模板文件的物理位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-30
    • 2023-03-04
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多