在页面内最后执行一个 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12155709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute a javascript function as last inside a page
提问by rvandoni
I need to be sure that a certain script within a page is executed as last. I thought of using JQuery
我需要确保页面中的某个脚本最后执行。我想过使用 JQuery
$(document).ready( ... )
but if there are more functions of this type, which is actually executed last?
但是如果这种类型的函数有更多,实际上最后执行的是哪个?
采纳答案by Quentin
There are many ways to delay the execution of a script.
有很多方法可以延迟脚本的执行。
There is no way to programatically detect all of them.
没有办法以编程方式检测所有这些。
For a reliable solution you would have to reverse engine the code of each page you care about, figure out what it is doing (and when) and write your delay script specifically for that page. (For a value of "reliable" equal to "until they change the page").
要获得可靠的解决方案,您必须对您关心的每个页面的代码进行逆向引擎,弄清楚它在做什么(以及何时)并专门为该页面编写延迟脚本。(对于“可靠”的值等于“直到他们更改页面”)。
回答by Constantinius
This depends on the order in which you have registered them.
这取决于您注册它们的顺序。
E.g:
例如:
$(document).ready( function() { alert("first"); });
$(document).ready( function() { alert("second"); });
$(document).ready( function() { alert("third"); });?
would alert "first" then "second" then "third"
会提醒“第一”然后“第二”然后“第三”
So adding a <script>
to the bottom of your page with an $(document).ready( yourfunction );
would suffice.
因此<script>
,在页面底部添加一个$(document).ready( yourfunction );
就足够了。
回答by Danil Speransky
Theoretically you can do something like this:
理论上你可以这样做:
// Form array of functions which sould be called with according order
$.readyArray = [function () { ... }, function () { ... }, function () { ... }, ...];
// just execute them in this order when onready event
$(document).ready(function() {
for (var i = 0; i < $.readyArray.length; i++) {
//apply, calls function in current context and pass arguments object
$.readyArray[i].apply(this,[arguments]);
}
});
回答by Cosmin
$(document).ready( ... )
is not executed last. The last function executed ( so, after document ready ) is the one(s) from <body onload>
.
不是最后执行的。执行的最后一个函数(所以,在文档准备好之后)是来自<body onload>
.
Example : <body onload="myJSfunction();">
例子 : <body onload="myJSfunction();">
Here, the javascript myJSfunction
is executed at the end, after $(document).ready( ... ).
在这里,javascriptmyJSfunction
在最后执行,在 $(document).ready( ... ) 之后。
回答by Ravi
In simple Javascript solution, you could call the javascript function at end of your HTML document inside the script tag. This works well when you are not using jQuery.
在简单的 Javascript 解决方案中,您可以在脚本标签内的 HTML 文档末尾调用 javascript 函数。当您不使用 jQuery 时,这很有效。
In case of jQuery you could use load method.The load event is sent to an element when it and all sub-elements have been completely loaded.
在 jQuery 的情况下,您可以使用 load 方法。当元素和所有子元素都已完全加载时,将向元素发送 load 事件。
For more info look at
有关更多信息,请查看
回答by Vishnu S
Try this,
试试这个,
$(window).bind("load", function() {
//code here
});