permaculture playing cards*
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


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "DIV containing a script" Watch "DIV containing a script" New topic
Author

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:


then...


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: http://24ways.org/advent/have-your-dom-and-script-it-too

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?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

eval();
[ 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
author
Rancher

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

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

+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
 
GeeCON Prague 2014
 
subject: DIV containing a script