www.webdeveloper.com
Results 1 to 3 of 3

Thread: On page load run script

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Posts
    34

    On page load run script

    Hi there,

    I am hoping to add some text to a div using the append method in JS.

    So far I ahve this:

    Code:
    function createDiv()
            {
         
                var pTag = document.createElement("p");            
                           
                pTag.innerHTML = "This paragraph <b>HTML p tag</b> is added dynamically inside the div tag.";
                 
                document.getElementById("ctl00_MiWebC_pnlLogin").appendChild(pTag);
            }
    How ever this code only works if there is an input button with an onclick event handler on it (createDiv().

    Is there a way I can get this script to automatically run without the onclick. I.e on page load.

    I cannot access the body html tag so I cannot set it there.

    Would something like this work ?

    Code:
    <script type="text/javascript" language="javascript">    
            {
                           
                var pTag = document.createElement("p");
                 
                pTag.setAttribute("align","center");
                
                pTag.innerHTML = "This paragraph <b>HTML p tag</b> is added.";
                 
                document.getElementById("div1").appendChild(pTag);
            }
     
    </script>
    Thank you all

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Code:
    window.onload=function(){
    // expressions, statements, functions, etc
    }

  3. #3
    Join Date
    Aug 2010
    Posts
    34
    Perfect ! thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles