innerHTML script

Discussion in 'JavaScript' started by Kofgarter, Jan 12, 2009.

  1. #1
    help me please, make work this script for Firefox! I know that it is possible, but don't know how to make it right =((

    <HTML>
    <script>
    function insertScript(){
    var sHTML="<input type=button onclick=" + "go2()" + " value='Click Me'><BR>";
    var sScript="<script DEFER>";
    sScript = sScript + "function go2(){ alert('Hello from inserted script.') }";
    sScript = sScript + "</SCRIPT" + ">";
    ScriptDiv.innerHTML = sHTML + sScript;
    }
    </SCRIPT>
    <BODY onload="insertScript();">
    <DIV ID="ScriptDiv"></DIV>
    </BODY>
    </HTML>

    sources:
    http://msdn.microsoft.com/en-us/library/ms533897(VS.85).aspx
    http://www.julienlecomte.net/blog/2007/12/38/
     
    Kofgarter, Jan 12, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    ScriptDiv.innerHTML = sHTML + sScript;

    wrong. use document.getElementById("ScriptDiv"):
    this is reported back in firefox, i don't think it can be any clearer... you may have to install firebug (www.getfirebug.com) to debug in firefox.

    also, the fact that you update an element's innerHTML does not evaluate the script automatically. in fact, i'd consider this a security issue - which is probably why if you fix the fatal error, you end up with "go2 is undefined".
     
    dimitar christoff, Jan 12, 2009 IP
  3. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #3
    Kofgarter, what is your intention with this script? Maybe we can help by writing the proper code so you can accomplish this.
     
    crath, Jan 12, 2009 IP
  4. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #4
    And remember to always put document. in front, if not - firefox and other browsers than IE wont parse it right :)
     
    elias_sorensen, Jan 12, 2009 IP
  5. manijifera

    manijifera Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you are doing it hard wasy
    
    
    function myhtml()
    
    {
    
    var html = "This is my text and this is my double quota \" add quota mark with slash and the quota mark like this - \" and then end of var with closed quota ";
    document.getElementById("mydiv").innerHTML = html;
    
    }
    
    <body onlaod="myhtml()">
    
    <div id="mydiv"></div>
    
    HTML:
     
    manijifera, Jan 14, 2009 IP
  6. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Should be:

    
    .......
    <script type="text/javascript">
    function myhtml(){
    var html = "This is my text and this is my double quota \" add quota mark with slash and the quota mark like this - \" and then end of var with closed quota ";
    document.getElementById("mydiv").innerHTML = html;
    }
    </script>
    </head>
    <body onload="myhtml()">
    <div id="mydiv"></div>
    .......
    
    HTML:
     
    elias_sorensen, Jan 14, 2009 IP
  7. TheLab

    TheLab Banned

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sorry for the bump, i just want to let all the members know that the fix by elias_sorensen is working! Thanks :D
     
    TheLab, Apr 18, 2009 IP
  8. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #8
    You're welcome :)
     
    elias_sorensen, Apr 18, 2009 IP
  9. onlywin

    onlywin Greenhorn

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #9
    some explanation about what is the idea of the script could help
     
    onlywin, May 21, 2009 IP
  10. luizeba

    luizeba Active Member

    Messages:
    265
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #10
    Agree with onlywin!
    Can someone explain the script, please?
     
    luizeba, May 24, 2009 IP