473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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.

Dec 7 '06 #1
2 1336
howa wrote:
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/comp.lang.javascript/browse_frm/thread/7e23f42490c301de/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>
--
Rob

Dec 7 '06 #2
In comp.lang.javascript message
<11*********************@16g2000cwy.googlegroups.c om>, Wed, 6 Dec 2006
23:27:12, RobG <rg***@iinet.net.auwrote:
>
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.

--
(c) John Stockton, Surrey, UK. REPLYyyww merlyn demon co uk Turnpike 6.05.
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm: about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
Dec 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Venus | last post by:
Hello, I am trying to generate a dynamic form at runtime and would like to do it using "<asp: ..." form elements as follows Build up the string that is placed somewhere in the HTML code the...
4
by: Venus | last post by:
Hello, Thanks for your reply. I understand that a control can be created dynamically in several ways: 1) using StringBuilder 2) using Controls.Add 3) using ASP PlaceHolder But this is just...
0
by: Venus | last post by:
Hello, After trying some ways to do it I wanted to use something like the code below but for some reason is not working (I have to generate the entire form dynamically (not only the controls)):...
0
by: Venus | last post by:
Hello, After trying some ways to do it I wanted to use something like the code below but for some reason is not working (I have to generate the entire form dynamically (not only the controls)):...
3
by: azegurb | last post by:
hi I have just took from internet dinamic table. this table is dynamic and its rows dynamically can be increased. but i would like how create SUM function that automatically sums each added row...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.