views:

706

answers:

6

hi, 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.

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

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.

Sarfraz
no this doesn't work in Dotnetnuke. Thanks
Romil Nagrani
thanks for letting me know about that.
Sarfraz
A: 
<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>
Romil Nagrani
Hi @romilnagrani. SO doesn't work like a forum. I've moved your code into your question (and formatted it). If you need to provide additional information, you can always edit your post to add it, but you shouldn't add it as an answer. It's likely to get down-voted that way.
Justin Johnson
-1 This is a terrible answer mate. I'm sure you can do better :)
roosteronacid
Rooster, see my comment.
Justin Johnson
@Justin: thanks for the advice. i would take care in future
Romil Nagrani
+2  A: 

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?

Justin Johnson
+1  A: 

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

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

David Dorward
+1  A: 

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.

ewalk