File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Problems using custom tag with javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Problems using custom tag with javascript" Watch "Problems using custom tag with javascript" New topic
Author

Problems using custom tag with javascript

Felipe Reis
Greenhorn

Joined: Oct 31, 2007
Posts: 20
Hey everyone.

I am trying to make a tree list to show a file directory standing on the server. As javascript is not really my area I've chosen to use a pre-existent function (http://www.destroydrop.com/javascripts/tree/) - which is really good by the way.
To pass the arguments to the javascript code, I am using a custom tag.

This is a chunk of the JSP:



and this is the generated code:



everything as expected... however the function never runs, and the tree list is never showed.

I created a JSP without the custom tag, with just the code generated by the 'real' JSP, and it worked just fine. That made me very confused...

Is there any problem with generating dynamic code in the header of the page? Or using custom tags with javascript? I am a bit lost...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Custom tags (and any JSP code for that matter) and Javascript exist in two totally different times and places so... no, there is no problem with using custom tags to generate Javascript and there is no problem using them in the head of an HTML page.

Think of it this way, your JSP code creates your HTML and Javascript code.
To the JSP side of the world HTML and Javascript are nothing more strings of text.


I see your Javascript array,
Where is the function?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Felipe Reis
Greenhorn

Joined: Oct 31, 2007
Posts: 20
That's what I thought... But my question is: Why when I copy the generated code into a new 'static' jsp, and make a request to that page the function works perfectly? At the end of the day is the same code, so why different results? That's why I assumed that there was a problem when using tags and javascript.

The function is in an external file. I can't understand it entirely (javascript simply doesn't make sense to me), and I can't post it here because of special characteres or something like that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

How are you including the JS file? That may be suspect.

See the entry in the JSP FAQ regarding resource URLs.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Also check to make sure you're not trying to call the function before the array is written to the page.
Felipe Reis
Greenhorn

Joined: Oct 31, 2007
Posts: 20
I've checked the FAQ about resources URL. As my page uses a static URL to include the js file I don't think that's the problem.

The elements are included in the array in the page <head> tag, so I think the function will be called just after that...

I'm stuck now. I'll do some more tests and see what I find out.

I've posted this question on a different forum, and a user suggested me to use scriptlets, because custom tags won't work. But that just doesn't make sense to me...

Thanks Bear and Ben.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Originally posted by Felipe Reis:
I've checked the FAQ about resources URL. As my page uses a static URL to include the js file I don't think that's the problem.

I'm not sure what you mean by "static" URL. Can you show us the URL? In fac t, how about showing us the code? (Please be sure to use UBB code tags).

The elements are included in the array in the page <head> tag, so I think the function will be called just after that...

Yeah, I definitely think that we need to see some code.

I've posted this question on a different forum, and a user suggested me to use scriptlets, because custom tags won't work. But that just doesn't make sense to me...

That is just about as flat out wrong as anything can be. Anything else this person says should be considered suspect.
Felipe Reis
Greenhorn

Joined: Oct 31, 2007
Posts: 20
Here is the code from the jsp:



This is the js file:

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Are you naming your array "Tree" or "DivArray"?




Also, when you create an array with the 'new' keyword in Javascript 'Array' needs parenthesis after it:
Example:


The name discrepancy wasn't there in the first piece of code you posted so I'm not sure if this is a red herring or not.
Felipe Reis
Greenhorn

Joined: Oct 31, 2007
Posts: 20
Oops, sorry... I've got 2 functions in that page, one with an array called Tree, and another called DivArray. But i've only posted the code relevant to the createTree() function.
Felipe Reis
Greenhorn

Joined: Oct 31, 2007
Posts: 20
Ben and Bear, thanks for the support.

I manage to solve the problem, and with the info that I provide, you guys could never have helped me. The problem was not in the code after all!

I have this habit of spending all my time looking for bugs in the code, and forgeting other issues like file location.

After some tests I found out that the function was not being called. The js file was in the same folder as my jsp, however, the jsp was called after a Servlet's forward(), so the URL in the browser bar was still from the first Servlet.

I spent days looking at the code, and the problem was solved after a glimpse on the browser bar...

Anyway, thanks again.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Glad it's working for you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

That was the whole point of the JSP FAQ entry I pointed yout to. Never use page-relative URLs.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems using custom tag with javascript