简体   繁体   English

window.load和document.ready的Javascript代码可用性

[英]Javascript code availability for both window.load and document.ready

I need a function that needs to be executed on document.ready and available to window.onload function ... 我需要一个需要在document.ready上执行并且可用于window.onload函数的函数...

How can I do that? 我怎样才能做到这一点?

$(document).ready(function someFunction() {
    execute something and pass the results to window.load
}
);


$(window).load(Need results of somefunction);​

You can share the result of a function call by assigning it to some variable in scope. 您可以通过将函数调用的结果分配给作用域中的某个变量来共享结果。

(function(){
   var result;
   $(document).ready(function(){
      result = 'something';
   });

   $(window).load(function() {
       console.log(result); //something
   });

}());

When passing a function() parameter to things like $(document).ready(fn) , you are actually creating an unnamed function, an anonymous function. 当将function()参数传递给$(document).ready(fn) ,实际上是在创建一个未命名的函数,一个匿名函数。 So, if you want to get the same result for both $(document).ready and $(window).load , you can create a named function (this is if you want to share the function itself): 因此,如果您想为$(document).ready$(window).load都获得相同的结果,则可以创建一个命名函数(这是您要共享函数本身):

function someFunction() {
    /* execute some code */
}

$(document).ready(someFunction);
$(window).load(someFunction);

Since I see that I've misunderstood your question firstly, let me solve that. 由于我发现我首先误解了您的问题,所以让我解决一下。

Having a variable in the scope of both functions will alow both of them to use it. 在两个函数的范围内都有一个变量将使它们两个都不能使用它。 So, since having a variable in the global namespace it's not a good practice, then we should use an unnamed functions to wrap your two functions inside it: 因此,由于在全局命名空间中包含变量不是一个好习惯,因此我们应该使用未命名的函数将两个函数包装在其中:

(function(){
  var accessible_var;
  $(document).ready( function() {
    accessible_var = "passed to load handler";
  } );

  $(window).load( function() {
    console.log(accessible_var);
  } );
})();

You have to take care with these two handlers! 您必须注意这两个处理程序!

document.ready will fire when all the elements are in place, and they can be referenced in the JS code, but the content is not necessarily loaded. 当所有元素都放置到位时, document.ready将触发,并且可以在JS代码中引用它们,但是不必加载内容。

The window.load , however will wait for the page to be fully loaded, this includes inner frames, images etc. window.load ,但是将等待页面完全加载,包括内部框架,图像等。

Warning - If you want to use the Height, Width properties of an image for example, then document.ready is a deal braker!! 警告 -例如,如果您想使用图像的高度,宽度属性,那么document.ready无疑是一个大难题!

(as it is written here ) (因为它是写在这里

Since you need to do something on dom ready, then access it on window load, nest the window load binding method inside the dom ready method. 由于您需要在dom ready上做一些事情,然后在窗口加载时访问它,因此将窗口加载绑定方法嵌套在dom ready方法内。

$(document).ready(function () {
    // do something
    var somevar = "Hello World!";
    $(window).on("load", function () {
        console.log(somevar); // Hello World!;
    });
});

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

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