包含基于当前页面的JS文件

时间:2011-03-03 18:41:57

标签: jsf richfaces facelets

我的主要jsf包装页面上有以下内容:

<head>
  <meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8"/>
  <title>
    <ui:insert name="pageTitle" /> 
  </title>

  <a4j:loadScript src="resource://jquery.js" />
  <a4j:loadScript src="resource:///bla/html/js/base.js" />
  <a4j:loadScript src="resource:///bla/html/js/common.js" />
  <a4j:loadScript src="resource:///bla/html/js/inactivity.js" />
</head>

现在我在网站上有一个新页面,它需要一些js功能。所以我创建了一个新的js文件myNewJSFile.js

如果用户登陆此页面,我只想加载它,所以很明显我不想将它添加到我上面的文件列表中。另外,我宁愿不把它包含在新页面的顶部。

我想要的是一个可以根据当前页面定义要加载的文件的地方。

这样的事情(伪代码):

主要的jsf包装器:

<head>
  <jsIncludes>
</head>


js包括文件:

<a4j:loadScript src="resource://jquery.js" />
<a4j:loadScript src="resource:///bla/html/js/base.js" />
<a4j:loadScript src="resource:///bla/html/js/common.js" />
<a4j:loadScript src="resource:///bla/html/js/inactivity.js" />

<if page == "myNewPage>
  <a4j:loadScript src="resource:///bla/html/js/myNewJsFile.js" />
<elfeif ...>
...
</if>

有一种简单的方法吗?

由于

2 个答案:

答案 0 :(得分:2)

a4j:loadScript始终将javascript放在<head>中,无论您在何处使用它,都必须执行任何操作。 (由于一些奇怪的原因,医生没有提到这一点)

回答您的问题:如果您在需要的页面中使用<a4j:loadScript src="resource:///bla/html/js/myNewJsFile.js" />,则<script style="text/javascript" src="..." />会显示在<head>

答案 1 :(得分:0)

我宁愿将另一个<ui:insert>添加到主模板的<head>

<head>
    ...
    <ui:insert name="headContent" />
</head>

然后在页面模板中,只需定义要插入的附加脚本。

<ui:composition template="main.xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:a4j="http://??? can't tell from top of head :)"
>
    <ui:define name="headContent">
        <a4j:loadScript src="resource:///bla/html/js/myNewJsFile.js" />
    </ui:define>

    ...
</ui:composition>
相关问题