Is there an event that I can register that will be executed after all $(document).ready
scripts are executed? I've got a piece of code that needs to be executed after the whole page is fully loaded (also the 开发者_开发百科jQuery scripts). Is this possible?
I need to register this in a script file, so can't put the function at the bottom of the page.
It is not possible to attach an event to when all of the scripts are executed and run. There is no hook for for your particular case in javascript because scripts can have very different logic like being loaded asynchronously, having timeouts, etc. But there is an event that is fired when all of the scripts are loaded in the browser (but still they are not executed):
$(window).load(function () {
// your code is here
});
will be executed after the whole page including scripts and graphics will be loaded. Though, this doesn't mean the scripts are actually executed. But, if your scripts only contain the logic that requires immediate actions like
alert('Hi!');
then those will be executed before $(window).load()
Yes, $(document).ready is executed after DOM loading complete
$(document).load(function() {
//script here });
This function will execute once all elements of a page have loaded. Including images.
http://api.jquery.com/load-event/
Or this:
(function() {
var myScript = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
myScript.src = 'http://mysite.com/script.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertAfter(myScript, s);
})();
This will insert an external script (on the fly) at the end of the page. It will be the last thing that's loaded.
The $(document).ready
scripts are executed in order. You can simply add a $(document).ready
handler to the bottom of the page.
EDIT
Note that this assumes your not using async
script loading
EDIT
If you need to include the function in a script file and the script file cannot somehow be the last one to load you could try to append the ready function on the fly (untested):
$(document).ready(function() {
$('body').append('<script>$(document).ready(...);</script>');
});
精彩评论