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?
 
b0lsc0ttConnect With a Mentor IT 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
 
erikTsomikConnect With a Mentor System 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
 
contactkarthiConnect With a Mentor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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
All Courses

From novice to tech pro — start learning today.