• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

AJAX Best Practices

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've recently created two AJAX widgets. One is a calendar object and another is an auto complete object. What I've found after creating both is that I am mixing alot of HTML into my JavaScript. In the future I could see this as being troublesome for maintenance and modifications. Are there guidelines available that might explain how to keep the two areas separate? Would it be possible to read a static file into the JavaScript and post the results with minor data changes? Is it possible to interact with taglibs using AJAX?
 
Author
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're have a lot of HTML in your JavaScript them I'm guessing you're creating new HTML using string concatenation in JavaScript. Is that correct?

One way to avoid this is to have the server generate the HTML you want to you and sent that back to the browser. Then, you could use the innerHTML property to update existing HTML with the HTML generated on the server.

Server side technologies like PHP, JSP, and ASP are generally much easier to work with when creating HTML content rather than string concatenation.
 
graham king
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right I'm basically recreating my HTML in JavaScript through string concatenations after I receive a response from my Servlet. With so many different technologies available it is sometimes hard to see how they all fit together. Let me try and explain how I got the calendar to work and maybe you can point me in a different\better direction.

I wrote a taglib to display a calendar (just what the world needs another calendar obj) only because the free one I got off the web mysteriously stopped working. The page is rendered and the taglib generates a hidden calendar object. When the user hits an img button the calendar shows.

Here's where AJAX comes into play. When the user selects one of the navigational << >> arrows the javascript is executed and a request is made to a calendar servlet. The servlet checks the params and sends an xml response to the callback function. Within this function the innerHTML attribute is replaced with the concatenated HTML string's.

I've read that the server's response can be in either plain text, xml or even a file. The only examples that I've found though have always sent a reponse back through xml. If I were to send a preformatted HTML reponse, then how, within javascript, would I access the data?

Thanks
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use responseText rather than responseXML to retrieve your HTML as text.
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic