www.webdeveloper.com
Results 1 to 7 of 7

Thread: Question about DIV and JavaScript

  1. #1
    Join Date
    Nov 2011
    Posts
    16

    Question about DIV and JavaScript

    I have this line of code:

    <div class="blue" onclick="myfunction()" id="blue2">

    This works fine and makes the JavaScript work if you click the Div.

    Is there a way to make it so you don't have to click it, but it works automatically upon page load?

    Thanks.

  2. #2
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,463
    Hi there davidwhite,

    does this help...
    Code:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <title>basic template</title>
    
    <style type="text/css">
    
    </style>
    
    <script type="text/javascript">
    (function() {
       'use strict';
    
    function myfunction() {
    
     }
    
       window.addEventListener?
       window.addEventListener('load',myfunction,false):
       window.attachEvent('onload',myfunction);
    
    })();
    
    </script>
    
    </head>
    <body>
    
    <div class="blue" id="blue2">
    
    </div>
    
    </body>
    </html>
    coothead

  3. #3
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    ... or just

    Code:
    <body onload="document.getElementById('blue2').click()">
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  4. #4
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,463
    Hi there Padonak,
    ... or just....
    I would suggest not.

    Further reading:-


    coothead

  5. #5
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    hello coothead )

    of course you are right, but we can use such things sometimes obtrusive or not for example when debugging smth or else
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  6. #6
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,463
    Hi there Padonak,

    You should also note that your example, unfortunately, does not seem to work in Safari 5.1.7.

    coothead

  7. #7
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <title>basic template</title>
    
    <style type="text/css">
    
    </style>
    
    <script type="text/javascript">
    function xx(){
    var divs=document.getElementsByTagName('div');
    for(var i in divs){divs[i].onclick=function(){alert(this.id||'no id defined! innerHTML = '+this.innerHTML);}}
    }
    </script>
    
    </head>
    <body onload="xx();document.getElementById('blue2').click()">
    
    <div class="blue" id="blue2">
    lol
    </div>
    <div class="blue">
    megalol
    </div>
    
    </body>
    </html>
    IE,FF,Chrome and Opera VS Safari

    4:1 Safari loose ))
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

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

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