我有一个jquery脚本,需要在页面中的其他所有内容(包括一些我无法控制的其他javascripts)完成其任务后才运行。
我考虑过是否有一种替代方法来使用$(document).ready,但是我没有找到。
我有一个jquery脚本,需要在页面中的其他所有内容(包括一些我无法控制的其他javascripts)完成其任务后才运行。
我考虑过是否有一种替代方法来使用$(document).ready,但是我没有找到。
在页面中可以多次使用$(document).ready()
。代码将按照它出现的顺序运行。
您可以使用$(window).load()
事件来编写代码,因为此事件发生在页面完全加载完成并且各种$(document).ready()
处理程序中的所有代码都已运行结束之后。
$(window).load(function(){
//your code here
});
这段代码解决了我的问题,
<script type="text/javascript">
$(window).bind("load", function () {
// Code here
});
</script>
多个$(document).ready()
将按照页面从上到下的顺序依次触发。 最后一个$(document).ready()
将在页面最后触发。 在最后一个$(document).ready()
中,您可以触发一个新的自定义事件来在所有其他事件之后触发。
将您的代码包装在新自定义事件的事件处理程序中。
<html>
<head>
<script>
$(document).on("my-event-afterLastDocumentReady", function () {
// Fires LAST
});
$(document).ready(function() {
// Fires FIRST
});
$(document).ready(function() {
// Fires SECOND
});
$(document).ready(function() {
// Fires THIRD
});
</script>
<body>
... other code, scripts, etc....
</body>
</html>
<script>
$(document).ready(function() {
// Fires FOURTH
// This event will fire after all the other $(document).ready() functions have completed.
// Usefull when your script is at the top of the page, but you need it run last
$(document).trigger("my-event-afterLastDocumentReady");
});
</script>
以下内容来自这里:
// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);
// Check if jQuery's loaded
function GM_wait()
{
if(typeof unsafeWindow.jQuery == 'undefined')
{
window.setTimeout(GM_wait,100);
}
else
{
$ = unsafeWindow.jQuery;
letsJQuery();
}
}
GM_wait();
// All your GM code must be inside this function
function letsJQuery()
{
// Do your jQuery stuff in here
}
<script type="text/javascript" language="JavaScript">
function js(url){
s = document.createElement("script");
s.type = "text/javascript";
s.src = url;
document.getElementsByTagName("head")[0].appendChild(s);
}
js("/js/jquery-ui.js");
js("/js/jrails.js");
js("/js/jquery.jgrowl-min.js");
js("/js/jquery.scrollTo-min.js");
js("/js/jquery.corner-min.js");
js("/js/jquery.cookie-min.js");
js("/js/application-min.js");
function JS_wait() {
if (typeof $.cookie == 'undefined' || // set in jquery.cookie-min.js
typeof getLastViewedAnchor == 'undefined' || // set in application-min.js
typeof getLastViewedArchive == 'undefined' || // set in application-min.js
typeof getAntiSpamValue == 'undefined') // set in application-min.js
{
window.setTimeout(JS_wait, 100);
}
else
{
JS_ready();
}
}
function JS_ready() {
// snipped
};
$(document).ready(JS_wait);
</script>
$().ready
载入所有初始化函数,然后运行你想要的jQuery函数?$().ready
函数上使用setTimeout()
,调用你想要加载的功能。setInterval()
,并让时间间隔检查其他所有载入函数是否已经完成(将状态存储在布尔变量中)。当满足条件时,你可以取消时间间隔并运行载入函数。$(window).load(function(){...})
。 - Ryan Taylormy_finalFunction
函数:<script>
document.addEventListener("load", my_finalFunction, false);
function my_finalFunction(e) {
/* things to do after all has been loaded */
}
</script>
$(window).on('load', function() { });
。 - Alex