0

I want to include a js file in my jsf page. i tried the usual way of doing it but it didnt work.

<script language="Javascript" type="text/javascript" src="js/alertMessages.js"></script>

I also tried the adf components as below: but there is no af:resource component in my adf lib.

<af:document>
  <f:facet name="metaContainer">
    <af:resource source="/mySourceDirectory"/>
  </facet>
  <af:form></af:form>
</af:document>

Please let me know how do i include the js file in my page.

Thanks...

user265950
  • 467
  • 3
  • 9
  • 21

2 Answers2

1

Relative resource URLs (the URLs of <script>, <link>, <img> and <a> elements which do not start with scheme http:// or slash /) are relative to the current request URL (as you see in browser address bar).

So if you're for example opening the page by

http://localhost:8080/contextname/somefolder/page.jsf

then the script file as you have there will actually be downloaded from

http://localhost:8080/contextname/somefolder/js/alertMessages.js

instead of

http://localhost:8080/contextname/js/alertMessages.js

You would like to make the URL relative to the domain root instead. So it must become /contextname/js/alertMessages.js. You can use ${pageContext.request.contextPath} to inline the context path dynamically:

<script type="text/javascript" src="${pageContext.request.contextPath}/js/alertMessages.js"></script>

Note that the language attribute is superfluous. Also please note that for JSF 2.x there are tags available which do that implicitly like <h:outputScript>.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

You should use the af:resource tag for including javascript in ADF Faces pages. http://docs.oracle.com/cd/E21043_01/apirefs.1111/e12419/tagdoc/af_resource.html

Shay Shmeltzer
  • 3,693
  • 1
  • 13
  • 9