Solved

JSP Document (JSPX) Page does not run JQuery

Posted on 2009-07-10
9
2,052 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 26

Expert Comment

by:mrcoffee365
Comment Utility
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
Comment Utility
You can download an example of this project from here: http://files.me.com/zenserve/78xjf4

0
 
LVL 26

Expert Comment

by:mrcoffee365
Comment Utility
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
Comment Utility
How is it that exactly the same JS code can be used by the JSP file without JavaScript errors?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 26

Expert Comment

by:mrcoffee365
Comment Utility
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
Comment Utility
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 26

Accepted Solution

by:
mrcoffee365 earned 500 total points
Comment Utility
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 26

Expert Comment

by:mrcoffee365
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

744 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

18 Experts available now in Live!

Get 1:1 Help Now