How to dynamic append and run a javascript in a section of HTML?

H

howa

e.g.


<div id="test">

test

</div>


<script>

document.getElementById("test").innerHTML
='<script>alert(1);</sc'+'ript>';

</script>

how to make the code run at the DIV - test ?

thanks.
 
R

RobG

howa said:
e.g.

<div id="test">
test
</div>
<script>
document.getElementById("test").innerHTML
='<script>alert(1);</sc'+'ript>';
</script>

how to make the code run at the DIV - test ?

There has just been an extensive thread on this very subject, although
the title may not indicate it:

<URL:
http://groups.google.com.au/group/c...42490c301de/f97dcd8d723274f7#f97dcd8d723274f7>

Essentially the most cross-browser way is to strip out the script
elements and eval their content. There are other (possibly better)
methods that may become viable in the future, but for now eval() seems
to be the best bet.

Note that it may have unexpected effects on the scope of declared
variables, ensure you understand what they are before doing anything
non-trivial.

You might like to try the FORK library's Mutate function which does
most of the hard work for you:

<URL: http://forkjavascript.org/ >

<script type="text/javascript" src="mutate.js"></script>

<div id="test">test</div>

<script type="text/javascript">
FORK.Mutate.insertBottom(
'test',
'<script type="text/javascript">alert(1);<\/script>'
);
</script>
 
D

Dr J R Stockton

In comp.lang.javascript message
Wed said:
There has just been an extensive thread on this very subject, although
the title may not indicate it:

For those with good off-line newsreaders, it would be helpful to give
such Titles.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top