dynamically add content

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tomeshjain
    New Member
    • Oct 2009
    • 1

    dynamically add content

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
      <title></title>
      <script >
        function fun1()
        {
    	document.body.innerHTML= "<input type=button value=clickme1 onclick='fun2()'>";
    	var code=document.head.script.innerHTML;
    	code+="function fun2() {document.write('hello'); } ";
    	document.head.body.script.innerHTML=code;
    	}
    </script >
    </head>
    <body  id="codes">
        <input type=button value=clickme onclick="fun1()" />
    
    </body>
    
    </html>


    i want to add a function directly in to the page ...how can it be done
    this above progam is not working ...plz help
    Last edited by Dormilich; Oct 21 '09, 12:09 PM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    first of all, validate your HTML (http://validator.w3.org)

    further, this code seems terribly outdated (so you might read a Javascript tutorial to get up to date)

    and I don’t understand what the code should do… you try to copy code around when you’re probably better off just executing the function.

    PS. please don’t hijack someone other’s thread if you have an unrelated question, just open a new thread.

    and… welcome at bytes.com

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      If you want to add JavaScript code to the head, try something like this:
      Code:
      var script = document.createElement("script");
      var text = document.createTextNode("code here");
      script.appendChild(text);
      document.getElementsByTagName("head")[0].appendChild(script);

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by tomeshjain
        Code:
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
          <title></title>
          <script > // missing type attribute
            function fun1()
            {
        	document.body.innerHTML= "<input [B]type=button value=clickme1[/B] onclick='fun2()'>";
        	var code=document.head.script.innerHTML;
        	code+="function fun2() {document.write('hello'); } ";
        	[B]document.head.body.script.[/B]innerHTML=code; // ?????
        	}
        </script >
        </head>
        <body  id="codes">
            <input [B]type=button value=clickme[/B] onclick="fun1()" />
        </body>
        </html>
        the bold marked XHTML code will cause an error. the bold marked javascript code is just wrong.
        you should also write your Javascript code in a CDATA section, otherwise an error will be thrown on every &, < and >.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by Dormilich
          you should also write your Javascript code in a CDATA section, otherwise an error will be thrown on every &, < and >.
          True, or put it in a file and include it.

          Comment

          Working...