0

I have a resources web app, which only has static content, such as images, CSS and JavaScript.

I'm using these resources in another web app within JSF pages, but for some reason the JavaScript files are not loaded properly. I've seen two outcomes so far, either the JSF page renders weirdly (missing most of the content) or the page looks fine, but the JavaScript is not functional. CSS and images come alright.

I'm loading the resources like below

<head>
    <link rel="stylesheet" type="text/css" href="http://localhost:8080/resources/css/styles.css" />
    <script type="text/javascript" src="http://localhost:8080/resources/js/utils.js" />
</head>

FireBug shows that everything is loaded properly. The JavaScripts work fine when they are inline inside the JSF page.

I've tried with Jetty 8 and GlassFish 3, but results are pretty much same. Anyone got some pointers on what's the problem?

kaskelotti
  • 4,709
  • 9
  • 45
  • 72

1 Answers1

2

Self-closing <script> tag is not valid in HTML documents with text/html content type and the browser behaviour is undetermined. You need to close it with another tag:

<script type="text/javascript" src="http://localhost:8080/resources/js/utils.js"></script>

It's however valid in pure XHTML documents with application/xhtml+xml content type, but this is in turn not supported by IE. Serving XHTML as text/html is in turn considered harmful.

This has nothing to do with Java/JSF.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the swift answer. I tried with the closing tags but that didn't help. When I look at the page source with the browser, it's changed back to self-closing tag. Even the attribute order is changed. I tried it on Jetty and GlassFish with both IE8 and FF8. – kaskelotti Nov 10 '11 at 21:09
  • 1
    What view technology are you using? The legacy JSPX has this irky behaviour. You'd need to add a comment `` between the tags. Or, better, dump it altogether and go for its awesome successor Facelets. – BalusC Nov 10 '11 at 21:11
  • Adding a comment between the tags didn't help. I guess I'll have to take a look facelets then. Thanks for the help. – kaskelotti Nov 10 '11 at 21:52
  • 1
    What view technology are you using? Is it indeed JSPX? Try `` instead of the comment. The comment seems to fail on some servletcontainers. See also related question: http://stackoverflow.com/questions/2058649/jspx-script-element-on-glassfish-v3 – BalusC Nov 10 '11 at 21:55
  • 1
    I misunderstood your previous comment and tried to add the comment block between the and tags. After reading the related question, I realized you meant to add it inside the – kaskelotti Nov 10 '11 at 22:30
  • And for the record, in case someone stumbles on this page with the same problem. Yes it is JSPX. – kaskelotti Nov 10 '11 at 22:31
  • You're welcome. I must admit that English is not my native, but how exactly was "add a comment `` between the tags" misinterpeted? – BalusC Nov 10 '11 at 22:49
  • Neither is mine. My first attempt was like – kaskelotti Nov 10 '11 at 23:00