Solved

JSP Document (JSPX) Page does not run JQuery

Posted on 2009-07-10
9
2,078 Views
Last Modified: 2013-12-02
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!
0
Comment
Question by:zenserve
  • 5
  • 4
9 Comments
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 24836593
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.
0
 

Author Comment

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

0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 24838646
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.
0
 

Author Comment

by:zenserve
ID: 24838745
How is it that exactly the same JS code can be used by the JSP file without JavaScript errors?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 27

Expert Comment

by:mrcoffee365
ID: 24838883
XML being run through XSLT applies different parsers.  Do you see the errors in the Error Console?  Are you able to fix them?
0
 

Author Comment

by:zenserve
ID: 24848769
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...
0
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 500 total points
ID: 24849594
Yes, the whole point is that if the file is .jspx, then a different set of parsers come into play.  Your code also runs as .jsp, for the same reason - if it's JSP, then the XML parser is not involved.

If you want the .jspx file to run as .jspx, then you have to work your way through all of the XML parsing issues.  It's a pain -- no ampersands, no less than or greater than symbols, etc.  That's probably what is causing the errors from the javascript which is executing -- it creates HTML, and those symbols are a central part of HTML.

This discussion gives some examples of the kinds of changes you have to make to make a .jspx document parse:
http://java.dzone.com/news/death-xslt-web-frameworks

This jspx documentation shows you what the .jspx page is translated into:
http://jspx.sourceforge.net/

Here's is Sun's tutorial on jspx (which you might have seen, but just in case):
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPX3.html

0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 24849615
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.
0
 

Author Comment

by:zenserve
ID: 24850246
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!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show confirmation alert in Jquery 14 41
JQuery serialize and unserialize 8 49
Replace &lt; with < 14 54
popup load of page and setting up of session 8 24
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now