Body onload in Javascript

10,347

Solution 1

I ran your code. Both methods executed without me having to make any modifications to the posted code. Is there possibly some other code that is overwriting the onload method?

Solution 2

The DotNetNuke best practice for binding to the "onload" property in JavaScript is to hook into JQuery's ready() method:

jQuery(document).ready( function() {
  // put your code here
  initVar();
  moveBanner();
}); 

DotNetNuke 4.9.x and later ship with the jQuery JavaScript library included.

Solution 3

Have you edited DNN's Default.aspx? Otherwise, there isn't any way for you to have access to the body tag to add the onload attribute like you show.

How are you injecting this script? Are you using a Text/HTML module, are you using the Page Header Text setting for the page, are you adding it directly to the skin, have you written a custom module, or something else?

Instead of using the onload attribute on the body tag, I would suggest wiring up to that event in the script itself. If you're using any code to inject the script, you can ask DNN to register jQuery or a ScriptManager (for ASP.NET AJAX) so that you can use those libraries to wire the event up easily. If you can't guarantee that those are on the page, use the following:

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function () {
    initVar();
    moveBanner();
});
Share:
10,347
xorpower
Author by

xorpower

Reached 500 Repo on May 22'11 Reached 600 Repo on Jul 29'11 Reached 700 Repo on Aug 10'11 Reached 800 Repo on Sep 09'11 Reached 900 Repo on Oct 13'11 Reached (& crossed) 1000 Repo during Mar 14-19'12 Reached 1300 Repo on May 8 2013

Updated on June 04, 2022

Comments

  • xorpower
    xorpower almost 2 years

    I had written one JS in asp.net. I had called that from body onload, but the JS doesn't get called where I have put my debugger. What could be possible reasons for this? I'm developing website in dotnetnuke.

    The JS I have written is syntactically and logically correct.

    <script type="text/javascript">
    
    var displayTime, speed, wait, banner1, banner2, link1, link2, bannerIndex, bannerLocations, bannerURLs;
    
    function initVar() {
        debugger;
    
      displayTime = 10; // The amount of time each banner will be displayed in seconds.
      speed = 5; // The speed at which the banners is moved (1 - 10, anything above 5 is not recommended).
      wait = true;
    
      banner1 = document.getElementById("banner1");
      banner2 = document.getElementById("banner2");
      //link1 = document.getElementById("link1");
      //link2 = document.getElementById("link2");
    
      //banner1 = document.getElementById("banner1");
      //banner2 = document.getElementById("banner2");
    
      banner1.style.left = 0;
      banner2.style.left = 500;
    
      bannerIndex = 1;
    
      /* Important: In order for this script to work properly, please make sure that the banner graphic and the
      URL associated with it have the same index in both, the bannerLocations and bannerURLs arrays.
      Duplicate URLs are permitted. */
    
      // Enter the location of the banner graphics in the array below.
      //bannerLocations = new Array("internet-lg.gif","jupiterweb.gif","jupitermedia.gif");
    
      bannerLocations = new Array("image00.jpg", "image01.jpg", "image02.jpg", "admin_ban.bmp");
    
      // Enter the URL's to which the banners will link to in the array below.
      bannerURLs = new Array("http://www.internet.com","http://www.jupiterweb.com","http://www.jupitermedia.com");
    }
    
    function moveBanner() {
        //debugger;
      if(!wait){
        banner1.style.left = parseInt(banner1.style.left) -  (speed * 5);
        banner2.style.left = parseInt(banner2.style.left) - (speed * 5);
        if(parseInt(banner1.style.left) <= -500){
          banner1.style.left = 500;
          bannerIndex = (bannerIndex < (bannerLocations.length - 1)) ? ++bannerIndex :0;
          banner1.src = bannerLocations[bannerIndex];
          //link1.href = bannerURLs[bannerIndex];
          wait = true;
        }
        if(parseInt(banner2.style.left) <= -500){
          banner2.style.left = 500;
          bannerIndex = (bannerIndex < (bannerLocations.length - 1)) ? ++bannerIndex :0;
          banner2.src = bannerLocations[bannerIndex];
          //link2.href = bannerURLs[bannerIndex];
          wait = true;
        }
    
        setTimeout("moveBanner()",100);
      } else {
          wait = false;
          setTimeout("moveBanner()", displayTime * 1000);
      }
    }
    
    </script>
    

    REGISTRATION IN JS

    <body onload="initVar(); moveBanner();">
    
    </body>