Link to home
Start Free TrialLog in
Avatar of zenserve
zenserve

asked on

JSP Document (JSPX) Page does not run JQuery

I have a web page, served by Tomcat, that's written using JSPX. The page contains a number of <div>s, each of which are hidden when it first loads. When the user clicks any of a number of different hyperlinks in the page, a JQuery function is executed to create a lightbox effect with the corresponding hidden <div> (essentially, each div can be displayed as a popup window).

However, when the page loads the <div>s are all visible, and nothing works. I'm getting a number of JQuery errors indicating that the browser had found problems within the main JQuery library code itself ("this._hide is undefined", "div.style is undefined").

What's extremely strange about this is that the JSP version of the page (that is, the non-XML version) works perfectly without any errors.

My initial assumption is that the DOM for a JSPX page is built in a different manner than one for a JSP page. Alternatively, perhaps this is something to do with the way JQuery works - I've noticed that if I reorder the various JQuery UI library file <script> entries within the page, the error changes, indicating that the function is executing before the rest of the libraries are loaded or the page is completely built.

But honestly, I've no idea. Any takers?

I will post code if necessary, but if someone can get a JSPX page that does something similar using JQuery working on Tomcat 6, that would be a start.

Thanks in advance!
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

You definitely should post code if you're still working on it.    The issue is probably that you're not creating the xml and xslt files correctly, rather than a JSP problem.  I suppose it could be JQuery, but again -- that doesn't change, so it's more likely an issue with xml and xslt.
Avatar of zenserve
zenserve

ASKER

You can download an example of this project from here: http://files.me.com/zenserve/78xjf4

The output from your .jspx page has a few Javascript errors.  In Firefox, look at the Error Console when you load the page.

If you fix those errors, then the divs might be hidden the way you want them to be.
How is it that exactly the same JS code can be used by the JSP file without JavaScript errors?
XML being run through XSLT applies different parsers.  Do you see the errors in the Error Console?  Are you able to fix them?
I see the errors - these are the ones I mentioned in my original post. The JQuery code is bug free, and works perfectly in the "this_works.jsp" file I sent.

I just noticed that if I rename the "this_does_not.jspx" file to "this_does_not.html" the JQuery works. Of course, if I do this now it does not compile as a JSP...
ASKER CERTIFIED SOLUTION
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I assume that you got those javascript libraries from somewhere else, so it's going to be a lot of work to make them work correctly for you in the .jspx page (that is, make them parse correctly for an XML parser).  It might be that you can't use those javascript libraries in a .jspx page, because it's too much work to convert them.
I took on board what you said and then had a think about what might be happening when the XML is parsed. This led to a eureka moment.

The issue was that the parser was turning empty elements such as <div></div> into <div/>. Adding a non-breaking white space (where permitted) fixes the problem; the DOM builds correctly and the JQuery executes without errors.

Many thanks!