« Road To Code Camp : Smart Client Data | Main | Oscar Misses the Boat »

January 27, 2006

Road Code Camp South Florida

So you are building dynamic HTML web pages. You've learned how to manipulate every part of the DOM. Every page you've written contains a flying toaster of some sort. ( And lets face it flying toasters is the pinnacle software evolution. Everything that came after that has pretty much been a down hill trend :-))

Now you need to write to script that doesn't produce HTML; it produces more script, but how do we do that? How do we get the browser to recognize a line of code after the page has rendered? The obvious way is to add a piece of HTML with an event declaration and line of JavaScript attached to it.

<div onclick="alert('Hello World');" >Click Here</div>

That works for simple script generation of a line or two as long as you don't mind being tied to an event. When you truly need script that will write script and execute it there is another way. The window object has a function called "eval". You create a line of code in the form of a string and pass it to the eval function. The eval function compiles and executes the line of code immediately.

var s = "alert('Hello World');"

eval(s);

10:04 AM | Permalink

Comments

The comments to this entry are closed.