javascript not executed from the innerHTML

Discussion in 'Javascript' started by Janooo, Dec 8, 2006.

  1. Janooo

    Janooo Guest

    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>
     
    Janooo, Dec 8, 2006
    #1
    1. Advertising

  2. Janooo

    ASM Guest

    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 ?

    --
    Stephane Moriaux et son (moins) vieux Mac déjà dépassé
    Stephane Moriaux and his (less) old Mac already out of date
     
    ASM, Dec 8, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    562
  2. rodchar
    Replies:
    2
    Views:
    585
    rodchar
    Apr 16, 2008
  3. Colin Colin
    Replies:
    2
    Views:
    107
    Colin Colin
    Apr 5, 2004
  4. teb
    Replies:
    0
    Views:
    98
  5. sonic
    Replies:
    5
    Views:
    293
    Randy Webb
    Jul 11, 2006
Loading...

Share This Page