English 中文(简体)
Body onload 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.

<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>
最佳回答

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!).





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签