Connecting Tech Pros Worldwide Help | Site Map

dynamically add content

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: 4 Weeks Ago
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5.  
  6. <head>
  7.   <title></title>
  8.   <script >
  9.     function fun1()
  10.     {
  11.     document.body.innerHTML= "<input type=button value=clickme1 onclick='fun2()'>";
  12.     var code=document.head.script.innerHTML;
  13.     code+="function fun2() {document.write('hello'); } ";
  14.     document.head.body.script.innerHTML=code;
  15.     }
  16. </script >
  17. </head>
  18. <body  id="codes">
  19.     <input type=button value=clickme onclick="fun1()" />
  20.  
  21. </body>
  22.  
  23. </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
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: 4 Weeks Ago

re: dynamically add content


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
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: 3 Weeks Ago

re: dynamically add content


If you want to add JavaScript code to the head, try something like this:
Expand|Select|Wrap|Line Numbers
  1. var script = document.createElement("script");
  2. var text = document.createTextNode("code here");
  3. script.appendChild(text);
  4. document.getElementsByTagName("head")[0].appendChild(script);
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: 3 Weeks Ago

re: dynamically add content


Quote:

Originally Posted by tomeshjain View Post

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6.   <title></title>
  7.   <script > // missing type attribute
  8.     function fun1()
  9.     {
  10.     document.body.innerHTML= "<input type=button value=clickme1 onclick='fun2()'>";
  11.     var code=document.head.script.innerHTML;
  12.     code+="function fun2() {document.write('hello'); } ";
  13.     document.head.body.script.innerHTML=code; // ?????
  14.     }
  15. </script >
  16. </head>
  17. <body  id="codes">
  18.     <input type=button value=clickme onclick="fun1()" />
  19. </body>
  20. </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 >.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5: 3 Weeks Ago

re: dynamically add content


Quote:

Originally Posted by Dormilich View Post

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.
Reply