#1
  1. a lazy guy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Hong Kong
    Posts
    99
    Rep Power
    13

    Javascript inside 'innerHTML'


    Hello,

    Is it possible to put a executable Javascript inside a <DIV> using method innerHTML ?

    I tried with the following code, but failed. Anyone got a solution?



    <html>
    <script>
    function test()
    {
    var str = 'xx\<script>alert("hello world");\<\/script>';
    ABC.innerHTML = str;
    }

    function read()
    {
    alert(ABC.innerHTML);
    }
    </script>

    <div id='ABC'></div>
    <a href="javascript:test()">test</a>

    <a href="javascript:read()">read</a>
    </html>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2001
    Posts
    765
    Rep Power
    13
    An 'executable Javascript' needs an event to cause it to be executed (once the page loads).

    <html>
    <head>
    <script>
    function test()
    {
    var str = '<a href="javascript:alert(\'hello world\')">test</a>';
    ABC.innerHTML = str;
    }
    </script>
    </head>
    <body>
    <div id="ABC"></div>
    <a href="javascript:test()">Insert</a>
    </body>
    </html>

IMN logo majestic logo threadwatch logo seochat tools logo