javascript not executed from the innerHTML

J

Janooo

Hi,
I am trying to understand why this code does not work as expected.
FF shows the message twice; IE doesn't show the message at all.
What would be the right way to get the message displayed only once?
Please, help, thanks...
Jano


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script language="javascript">
function test_of_innerhtml(){
document.getElementById('n1').innerHTML='<strong
onmouseout="javascript:alert(\'Hi\');">hi in</strong>';
}
</script>
</head>
<body>
<div id='n1' onmouseover="javascript:test_of_innerhtml();">hi</div>
<br />
<a
href="javascript:alert(document.getElementById('n1').innerHTML);">Show
innerHTML</a>
</body>
</html>
 
A

ASM

Janooo a écrit :
Hi,
I am trying to understand why this code does not work as expected.
FF shows the message twice; IE doesn't show the message at all.
What would be the right way to get the message displayed only once?
Please, help, thanks...

http://stephane.moriaux.perso.wanadoo.fr/truc/innerHTML_danger

Try this and probably will you see what happens :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<style type="text/css">
div, strong { border:1px solid red; padding: 2px 8px; }
</style><head>
<title>Untitled Page</title>
<script language="javascript">
var i = j = 0;
function test_of_innerhtml(){
document.getElementById('n1').innerHTML='<strong
onmouseout="javascript:alert(\'Hi_\'+i);i++">hi in '+j+'<\/strong>';
j++;
}
</script>
</head>
<body>
<div id='n1' onmouseover="javascript:test_of_innerhtml();">hello</div>
<br />
<a
href="javascript:alert(document.getElementById('n1').innerHTML);">Show
innerHTML</a>
</body>
</html>

When mouse leaves tag strong it leaves too the div reinserting a new
strong leaved as soon as created (2 tags strong in one).
To avoid double alert, probably with a counter could it be possible ?
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top