jQuery equivalente de body onLoad

Is it allowed to use <body onLoad="myfunc()"> along with jQuery's document.ready() handlers? I can't find a way to achieve the same functionality of the <body onLoad> with jQuery.

An example of a use case would be a facebook application. An Iframe facebook app requires the use of the FB.Canvas.setSize function which resize the iframe.

I would need to fire it up only when all elements on the page are finished loading.

preguntado el 09 de enero de 11 a las 08:01

6 Respuestas

$(window).load(myfunc) is what you're looking for. If you're working with the load event of an iframe, you can replace window with the iframe element/selector.

Respondido el 09 de enero de 11 a las 11:01

In case I create the iFrame dynamically later on, it seems like it is not possible to use .live() and load.. is there any solution for that? - Joel

After creating the iframe (e.g. var iframe = document.createElement('iframe');), you can pass it a load event callback with $(iframe).load(myfunc);. I would recommend doing this before attaching it to the DOM and/or setting its src atributo. - Kevin

I tried using both $(window).load(FB.Canvas.setSize()) and jQuery(document).loadFB.Canvas.setSize()) . However, none of them resize the window as opposed to using <body onload="FB.Canvas.setSize()"> which does the job. It seems like they act differently - Joel

Neither of those would work. You want something like this: $(iframe).load(function() { FB.Canvas.setSize(); }); If this still doesn't work, please edit your original question and provide more code/context. - Kevin

Understood. You can attach the load event callback to the window before creating the iframe: $(window).load(function() { FB.Canvas.setSize(); }); // then create iframe - Kevin

Esto también funciona:

    // ..
    // ..

Respondido 27 Jul 12, 00:07

How does this improve the accepted answer? There's much less verbose methods to do this.... - nombre_usuario_aleatorio

From the jQuery API on the ready manipulador:

The .ready() method is generally incompatible with the <body onload=""> attribute. 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.

Respondido el 09 de enero de 11 a las 11:01

.load(), .unload() or .error() are deprecated since jQuery 1.8. Lookup for these aliases in your code and replace them with the .on() method instead

$(window).on('load', function(){...})

Respondido 20 Oct 20, 18:10

Link to deprecated evento de carga. - Osmund Francis

Link to JQuery 3.0+ AJAX short-hand method called load(); readers should be aware of the difference when looking at their code (and resulting errors). - Osmund Francis

Found this thread updating some old code that had <body onload="someFunc()"> so to give an updated answer:

No use load(). Mientras $(document).ready(function(){...}); should work (depending on your use case) you actually want: $(window).on('load', function(){...}); to more closely mimic <body onload="...">

Some threads suggest this has been deprecated since jQuery 1.8 (as well as .unload() y .error()) and should be replaced with proper .on() implementations. I've confirmed that using load() on 3.3.1 results in an error, namely: indexOf is not a function in jquery.min.js

Documentation For jQuery's onControlador de eventos

respondido 21 mar '19, 17:03

pensé $(window).on('load', function () {...}) was the same thing as $(window).load(function() {...}). Are they not? - Jarad

Well they function similarly, however load() & on() are indeed two different & unique functions. I was just cautioning that I had found some sources stating the 'load'/'unload' were being phased out in favor of the more flexible 'on' function. - ThatOneGuy

Trata load():


Respondido 29 Oct 12, 04:10

I don't think you want to be passing the return value of myfunc hasta el load event ;) - Kevin

Please see my last comment to Kevin's answer - Joel

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.