Execute JavaScript with onclick="document.getElementById...

This does not work:
       onclick="document.getElementById('message').innerHTML = '<script>alert()</script>'; return false"

How can I insert JavaScript code into an element?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Demo</title>
</head>
<body>
 
<p style="height: 100px; width: 300px; border: 1px dotted red"><span class="message" id="message"></span></p>
 
<div><a href="#" onclick="document.getElementById('message').innerHTML = 'hello'; return false">Click here for "Hello"</a></div>
 
<div><a href="#" onclick="document.getElementById('message').innerHTML = '<script>alert()</script>'; return false">Click here to exicute JavaScript</a></div>
 
</body>
</html>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

erikTsomikSystem Architect, CF programmer Commented:
try this


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Demo</title>
</head>
<body>
 
<p style="height: 100px; width: 300px; border: 1px dotted red"><span class="message" id="message"></span></p>
 
<div><a href="#" onclick="document.getElementById('message').innerHTML = 'hello'; return false">Click here for "Hello"</a></div>
 
<div><a href="#" onclick="document.getElementById('message').innerHTML = alert(); return false">Click here to exicute JavaScript</a></div>
 
</body>
</html>

Open in new window

0
contactkarthiCommented:
if you want to alert the value inside the tag
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Demo</title>
</head>
<body> 
<p style="height: 100px; width: 300px; border: 1px dotted red"><span class="message" id="message"></span></p> 
<div><a href="#" onclick="document.getElementById('message').innerHTML = 'hello'; return false">Click here for "Hello"</a></div> 
<div><a href="#" onclick="alert(document.getElementById('message').innerHTML); return false">Click here to exicute JavaScript</a></div> 
</body>
</html>

Open in new window

0
hankknightAuthor Commented:
Thank you!

How can I do something like this, with text BEFORE and AFTER the JavaScript?

           "header text <script>alert('Hello')</script> footer text "
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

b0lsc0ttIT ManagerCommented:
hankknight,

The issue is putting script in html using the dom won't execute it.  The script will actually appear but it might as well just be html since the browser doesn't really see it as script.  That is why alerting the innerHTML should be done as contactKarthi showed above.  If you need to insert html or text then do that with innerHTML.  If you need to run script it has to be done outside of the innerHTML property.  You may need to use eval() in some cases but otherwise, for the last thing you posted, just do ...

onclick="document.getElementById('message').innerHTML = 'header text   footer text'; alert('Hello');"

The alert in the middle of the html will not be used.  If you want to add some of the html to the alert or do something different then let us know but I hope this helps explain why it seems like the script is going in when you use innerHTML.

Let me know if you have any questions or need more information.

b0lsc0tt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hankknightAuthor Commented:
Thanks for the information!
0
b0lsc0ttIT ManagerCommented:
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.