PDA

View Full Version : How to write Script block to a Div and Execute it


prakashtss
01-19-2008, 04:03 AM
I am trying to change the Script Block in a Div and Execute the Block.
I am able to write the Script Block, But not sure, How to invoke the Script.

It is cruicial, I execute the Script after writing to it.

Any Ideas ?? .

Here is the sample Code, I am trying to Execute

<div id="TryDIv">
<script>
document.write('Written when doc is loaded');
</script>
</div>
<input type=button value='DoIt' onclick='DoIt()'>

<script>
function DoIt()
{
var myDiv= document.getElementById('TryDIv');
myDiv.innerHTML='<scr'+'ipt>document.write("written after Button Click")</scr'+'ipt>';}</script>

shyam
01-19-2008, 12:27 PM
if u just want to add a piece of text into the div why not simply use the innerHTML to insert the actual text instead of using document.write...document.write is rendered only once

prakashtss
01-20-2008, 07:58 PM
Well!..My server gives a documen.write Response as a string. Which works good, when I use it in Document Load. But If the Call needs to change later on Client Side. this is the only way I can do It.

mjlorbet
01-20-2008, 08:31 PM
not true, you can just store the value you need in a "global" string and use the eval statement to actually perform the event unless you are talking about installing functions in which case you would use eval("var funcName = new Function(params){...}") or eval("var funcName = function(params){...}") to install your function