8

I'm creating a dynamic web project in Eclipse (almost from scratch) and I created a JSPX file where I put

<head>...
<script type="text/javascript" src="route/to/scripts/jquery.js"></script>
<script type="text/javascript" src="route/to/scripts/jquery.ui.js"></script>
<script type="text/javascript" src="route/to/scripts/something.js"></script>
</head>

I intend to use Jquery UI sortable and I found out that using JSPX, only the first script loads in Firefox and IE (while in opera it works...). If I use plain JSP, whether HTML of XHTML, it loads all the JS files.

Is there any way to include all the JS files successfully without using

<script>
<jsp:include ...>
</script>

that I must be aware of? (because this one loads the script INTO the final (X)HTML)

EDIT: Just thinking... why does Opera read the xhtml right while FF and IE failed at reading the <script> tags? Could it be a bug?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Alfabravo
  • 7,493
  • 6
  • 46
  • 82

1 Answers1

11

JSPX has the quirky behaviour that it auto-collapses tags without body. So effectively

<script type="text/javascript" src="route/to/scripts/jquery.js"></script>
<script type="text/javascript" src="route/to/scripts/jquery.ui.js"></script>
<script type="text/javascript" src="route/to/scripts/something.js"></script>

will end up in browser as

<script type="text/javascript" src="route/to/scripts/jquery.js" />
<script type="text/javascript" src="route/to/scripts/jquery.ui.js" />
<script type="text/javascript" src="route/to/scripts/something.js" />

which is invalid <script> syntax (rightclick page in browser and do View Source to see it yourself). The browser behaviour is undetermined.

You can workaround this by putting a <jsp:text /> between the tags

<script type="text/javascript" src="route/to/scripts/jquery.js"><jsp:text /></script>
<script type="text/javascript" src="route/to/scripts/jquery.ui.js"><jsp:text /></script>
<script type="text/javascript" src="route/to/scripts/something.js"><jsp:text /></script>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • So, it's quite the same trick required on IE6 that required some kind of &nbsp,   or so. I'll try it now hehe – Alfabravo Nov 29 '11 at 14:19
  • Inside ``, you mean? Well, that has actually a different cause :) – BalusC Nov 29 '11 at 14:21
  • Nope, in some app over Cocoon I worked, people put the tag with closing tag but without anything between and the serialized stuff was apparently fine. Somehow, IE6 failed to get the JS unless some spaces (" ") were added between the tags for each script. Also happened with "a" tags with background image and no text, whose image didn't show unless some spaces were added as well. – Alfabravo Nov 29 '11 at 14:56
  • Worked great. This trick also relieved me from a "new" bug due to autocollapsing divs that became invalid, bringing all the positioning to the ground (sheesh). Thanks for the advice – Alfabravo Nov 29 '11 at 16:13