Javascript 中的正文加载

发布于 2024-08-16 14:37:42 字数 2618 浏览 1 评论 0原文

我在asp.net中写过一个JS。我已经从 body onload 调用了它,但是 JS 没有在我放置调试器的地方被调用。造成这种情况的可能原因是什么?我正在 dotnetnuke 中开发网站。

我写的JS在语法和逻辑上都是正确的。

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

JS 注册

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

</body>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

累赘 2024-08-23 14:37:42

我运行了你的代码。这两种方法都无需我对发布的代码进行任何修改即可执行。是否可能有其他代码覆盖 onload 方法?

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?

猫卆 2024-08-23 14:37:42

在 JavaScript 中绑定到“onload”属性的 DotNetNuke 最佳实践是挂钩 JQuery 的 read() 方法:

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

DotNetNuke 4.9.x 及更高版本附带了 jQuery JavaScript 库。

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.

紫﹏色ふ单纯 2024-08-23 14:37:42

你编辑过DNN的Default.aspx吗?否则,您无法访问 body 标记来添加 onload 属性,如您所示。

你如何注入这个脚本?您是否使用文本/HTML 模块、是否使用页面的页眉文本设置、是否将其直接添加到皮肤、是否编写了自定义模块或其他内容?

我建议不要在 body 标记上使用 onload 属性,而是在脚本本身中连接到该事件。如果您使用任何代码来注入脚本,您可以要求 DNN 注册 jQuery 或 ScriptManager(对于 ASP.NET AJAX),以便您可以使用这些库轻松连接事件。如果您不能保证这些内容出现在页面上,请使用以下命令:

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

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();
});
诗酒趁年少 2024-08-23 14:37:42

我对asp.net了解不多,但如果您可以在页面中放置javascript代码,那么您可以尝试这种替代方法:

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

这与您在body标记中放置的内容相同。

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.

萌面超妹 2024-08-23 14:37:42

CSS left 属性采用长度,而不是整数。非零长度必须有单位。 (即使使用 JavaScript 设置它!)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文