aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Ajax - innerHTML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Ajax - innerHTML" Watch "Ajax - innerHTML" New topic
Author

Ajax - innerHTML

Daniel Cardenas
Greenhorn

Joined: Jul 26, 2005
Posts: 10
In Ajax apps, which is the risk of using innerHTML to show content?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

What risks have you heard of? Is there any substantive reasoning behind your question?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kris Hadlock
Author
Greenhorn

Joined: Nov 14, 2006
Posts: 26
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.


<a href="http://www.krishadlock.com" target="_blank" rel="nofollow">www.krishadlock.com</a><br /><a href="http://www.krishadlock.com/blog" target="_blank" rel="nofollow">www.krishadlock.com/blog</a><br /><a href="http://www.studiosedition.com" target="_blank" rel="nofollow">www.studiosedition.com</a><br /><a href="http://www.amazon.com/exec/obidos/redirect?link_code=as2&path=ASIN/0672329123&tag=krishadlock-20&camp=1789&creative=9325" target="_blank" rel="nofollow">Ajax for Web Application Developers</a>
Daniel Cardenas
Greenhorn

Joined: Jul 26, 2005
Posts: 10
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

Joined: Nov 14, 2006
Posts: 26
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

Joined: Jan 10, 2002
Posts: 60046
    
  65

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

Joined: Jul 26, 2005
Posts: 10
Thanks a lot! Kris and Bear.
I'm considering your opinions.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

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.


GenRocket - A Test Data Generation Platform
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ajax - innerHTML
 
Similar Threads
Obtaining data from form elements
Auto Refresh For Table Cells
Ajax implementation
Passing Value from Javascript method into HTML Body
innerHTML and script in AJAX