File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes DIV containing a script 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 "DIV containing a script" Watch "DIV containing a script" New topic

DIV containing a script

Dan Howard
Ranch Hand

Joined: Feb 22, 2004
Posts: 47
Hi all,

What i'm trying to do is have my server side code return the contents of a DIV. This chunk of html code contains a script which is dynamically generated on the server.

The ajax code looks like this in my script:


gridContainer is a table containing the script including all the data that's to be displayed.

First I had a problem because the script would not execute when I assigned the innerHTML property. I found a hack to solve this at:

This sort-of works. When I initially load the page the script executes but when I assign the innerHTML the *old* script executes. Not the new one with the updated data.

Does any of this make sence?

How do the experts handle dynamic scripts sent back from the XMLHttpRequest?

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60046

[ June 26, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dan Howard
Ranch Hand

Joined: Feb 22, 2004
Posts: 47
Hi Bear,

Yes. I can do that but the returned text is a mix of html and a script. Should I try to make it pure javaScript and eval. Maybe I can parse out the script.
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15376
look at prototype.js and it has an evalScript option with its Ajax code

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60046

+1 to using prototype.js.

I rarely write my own Ajax code anymore (unless I'm doing something fairly complex), and the script evaluation feature that Eric mentions will save you the time and effort of isolating the script from the HTML and eval-ing it on your own.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

For another similar approach suggested by Yuriy Fuksenko, see Execute JavaScript function present in HTML fragment loaded using AJAX

The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
I agree. Here's the link:
subject: DIV containing a script
Similar Threads
Eric, come in please
executing a javascript function from a servlet
disable submit button help
Help me to have a sortable table in my html profile!
Multiple Concurrent AJAX Connecitons, HOWTO?