• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ajax - innerHTML

 
Daniel Cardenas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Ajax apps, which is the risk of using innerHTML to show content?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What risks have you heard of? Is there any substantive reasoning behind your question?
 
Kris Hadlock
Author
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None, it is typically just used to display HTML on the client-side of the application. Unless you read the innerHTML and send the content to the server, then you would simply need to take the typical precautions in your server-side code.
 
Daniel Cardenas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is better to create a table?

Send a built HTML from server and put into web page with innerHTML or
send a XML/JSON file and iterate(creating) with javascript childnodes?
 
Kris Hadlock
Author
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,

Good question. It really depends on the situation, I would recommend to always go with the solution that provides you the control that you need. For example, if you are planning on doing a lot of manipulation to this table with JavaScript once it is built I would say to build it with JavaScript. If you are simply adding it to the page, I would say to go with the server-side solution.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And a combination approach (server-side to initially create the construct, and client-side objects to control it) is also possible and frequently the best of both worlds.
 
Daniel Cardenas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! Kris and Bear.
I'm considering your opinions.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
And a combination approach (server-side to initially create the construct, and client-side objects to control it) is also possible and frequently the best of both worlds.


+1.

I have been working recently on a very complex piece for a page that is largely controlled by Ajax calls to the server. The data coming back to the client was farily complex and I was finding a lot of business logic in JavaScript, which I detest.

My other option which I tried was creating all the HTML in server side code and then passing the HTML from a StringBuilder back to the client to simply pop in a DIV with innerHTML. This worked, but the intrececies (Sp?) of generating HTML in Java code was too much and would soon become a nightmore to maintain. Reminded me of some old school web development before the big MVC boom. Then Bear suggested something that I was finally able to try out and it worked great!

Ajax calls one of my actions on the server. The action forwards the request to a JSP which I use to render the HTML. This response is sent back to the browser in the ajax object which I then simply pop onto the page using innerHTML.

This solution is great because I can leverage the power of JSP's (JSTL, EL, etc) while still providing a clean maintainable template for all my ajax responses. If possible, I'll never deal with XML in JavaScript again.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose a next logical concern about the above approach is, if we are transmitting HTML back to the client, and it's a lot, then why not just refresh the entire page?

Typically, the amount of HTML sent back to the client is less than what would be required on a full page refresh or typically request/response. Also, I am in a situation where there is a Java applet running some proprietary flash app that I don't want to refresh everytime the page loads.

The other option was HTML Frames. Bleh! I just threw up in my mouth a little.

As always, weight the pros and cons of any ajax solution to make sure you aren't using it to be using it and making things more difficult for yourself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic