• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems using custom tag with javascript

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Felipe Reis
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you including the JS file? That may be suspect.

See the entry in the JSP FAQ regarding resource URLs.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also check to make sure you're not trying to call the function before the array is written to the page.
 
Felipe Reis
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code from the jsp:



This is the js file:

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad it's working for you.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was the whole point of the JSP FAQ entry I pointed yout to. Never use page-relative URLs.
reply
    Bookmark Topic Watch Topic
  • New Topic