body onload doesn't work in Javascript

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.

  • System.NullReferenceException: Object reference not set to an instance of an object. Dotnetnuke module creation
  • How to use asynchronous (non-blocking) javascript with DNN Client Resource Management
  • DNN Client Side Variables
  • How can i find source of html
  • DotNetNuke include javascript on every page
  • How to link JS file with modules, controls & templates in dotnetnuke?
  • Thanks

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

  • What is the easiest way to call a C# method from Javascript
  • DNN Client Side Variables
  • DotNetNuke include javascript on every page
  • AngularJS form in develop module of dotnetnuke 7
  • How can i find source of html
  • How to completely refresh (ctrl+f5) a page using asp.net?
  • 5 Solutions collect form web for “body onload doesn't work in Javascript”

    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?

    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.

    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();
    });
    

    I don’t know much about asp.net but if you can put javascript code in your page, then you can try this alternative:

    window.onload = function()
    {
      // any code here
    }
    

    This is the same as what you put in body tag.

    The CSS left property takes a length, not an integer. You must have units for non-zero lengths. (Even when setting it using JavaScript!).