简体   繁体   English

JavaScript onload函数未触发

[英]JavaScript onload function not firing

Let me explain what I would like to do. 让我解释一下我想做什么。 I have developed a web app and before my animation runs I would like to make sure that the images used in the animation have been loaded into the browser. 我已经开发了一个Web应用程序,在运行动画之前,我想确保动画中使用的图像已加载到浏览器中。 As such here is my JavaScript: 因此,这是我的JavaScript:

$(document).ready(function(){

    $.mobile.loading( 'show', {
                                text: 'Downloading Web App',
                                textVisible: true,
                                theme: 'a',
                                html: ""});

    document.getElementById('Logo').onload = function()
    {

        if(KeepCount == 0)
        {
            KeepCount++;
        }
        else if(KeepCount == 1)
        {
            KeepCount = 0;
            $.mobile.loading( 'hide' );
            StartSplash();
        }
    };

    document.getElementById('Hand').onload = function(){

        if(KeepCount == 0)
        {
            KeepCount++;
        }
        else if(KeepCount == 1)
        {
            KeepCount = 0;
            $.mobile.loading( 'hide' );
            StartSplash();
        }
    };
});

KeepCount is declared globally. KeepCount是全局声明的。 I've checked this in chromes developer tools and somehow the onload functions are never fired for some reason. 我已经在chrome开发人员工具中检查了这一点,由于某种原因,从来没有触发过onload函数。 Here is the HTML: 这是HTML:

 <img id='Logo' class="logo objecttransition"  src="css/images/Logo.gif">
    <img  id='Hand'  class="hand objecttransition"  src="css/images/hand.gif">

Any ideas? 有任何想法吗?

If you declare this in .ready scope, i guess that kind of late. 如果您在.ready范围内声明此内容,那么我猜想晚了。

Maybe load your images by javascript, then you can be sure they are ready ! 也许通过javascript加载图片,那么您可以确定它们已经准备好了!

function startLoadImages(){
 var image = new Image();
 image.load = function(){
  // i am done, inject me !!
 }
 image.src = "css/images/Logo.gif"
}

$(document).ready(function(){
  startLoadImages();
});

I would have to agree with Jonathan. 我将不得不同意乔纳森。 The jQuery docs for the .ready handlers located here , state the following. 此处.ready处理程序的jQuery文档声明如下。

...While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received... ...虽然JavaScript为呈现页面时执行代码提供了load事件,但是直到完全接收到所有资产(例如图像)之后,才会触发此事件...

The .ready() method is generally incompatible with the attribute. .ready()方法通常与属性不兼容。 If load must be used, either do not use .ready() or use jQuery's .load() method to attach load event handlers to the window or to more specific items, like images. 如果必须使用加载,请不要使用.ready()或使用jQuery的.load()方法将加载事件处理程序附加到窗口或图像等更具体的项目。

If you wanted to make sure images were loaded first, then you may want to use 如果要确保首先加载图像,则可能要使用

$(window).load(function()
{
  ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM