简体   繁体   English

如何添加处理程序以在呈现HTML时执行?

[英]How to add a handler to execute when HTML is rendered?

I am writing a one page web-application in JavaScript with jQuery . 我正在用jQueryJavaScript编写一个单页Web应用程序。 I add my JavaScript source and jQuery to the HTML page and register my function to be invoked when the the page is fully rendered. 我将JavaScript源和jQuery添加到HTML页面中,并注册页面完全呈现时要调用的函数。

<script type="text/javascript" src="lib/jquery-1.8.0.js"></script>
<script type="text/javascript" src="src/myapp.js"></script>
<script type="text/javascript">$(MyApp.onReady)</script>

It works but I do not like to have code inside HTML. 它可以工作,但我不喜欢在HTML中包含代码

Now I wonder if I can get rid of the last line. 现在,我想知道是否可以摆脱最后一行。 Is it absolutely necessary to invoke $(MyApp.onReady) in the HTML? 在HTML中调用$(MyApp.onReady)是否绝对必要?

You can respond to a document.ready in your myapp.js file. 您可以在myapp.js文件中响应document.ready This will ensure that the DOM tree is ready for use: 这将确保可以使用DOM树:

$(document).ready(function () {
    // do whatever you want here, confident that the DOM is loaded
});

If you need to wait until the entire page is rendered in the browser (images and such), you can respond to the load event: 如果您需要等到整个页面在浏览器中呈现(图像等)后,您可以响应load事件:

$(window).load(function() {
    // everything is rendered in the browser
});

You usually do not need to wait until load , i would recommend trying the first snippet before resorting to the second. 您通常不需要等到load ,我建议您在尝试使用第二个代码段之前先尝试第一个代码段。

$(document).ready is called when all the rendering on html controls is completed, You can put your code in this method or call your method in it $(document).ready在html控件上的所有呈现完成时被调用,您可以将代码放入此方法中,也可以在其中调用方法

$(document).ready(function(){
    //your code here
    //$(MyApp.onReady) 
});

You can place the $( MyApp.onReady ); 您可以放置$( MyApp.onReady ); line inside myapp.js . myapp.js行。 All you need to take care is to load jQuery before you load your file. 您需要注意的是在加载文件之前先加载jQuery

You can add this code to the tail of your myapp.js file like: 您可以将此代码添加到myapp.js文件的末尾,如下所示:

/* myapp.js */
var MyApp = (function(app, $) {
    // your logic...
    // defining app.onReady function...
    $(function() {
        app.onReady();
    });

    return app;
})(MyApp || {}, jQuery);

You need to call MyApp.onReady() somewhere. 您需要在某个地方调用MyApp.onReady() If you will be accessing the DOM (which you probably will be), yes, you must wait until the DOM is processed. 如果要访问DOM(可能将要访问),是的,您必须等待DOM处理完毕。 You can avoid having to use document.ready by putting MyApp.onready() in a script right before the closing body tag. 您可以通过将MyApp.onready()放在script MyApp.onready()在结束body标签之前的位置来避免使用document.ready By then, the DOM will have already been parsed. 届时,DOM将已经被解析。 However, if you aren't accessing the DOM, you can just do MyApp.onReady() anywhere. 但是,如果您不访问DOM,则可以在任何地方执行MyApp.onReady()

Take a look at this question: Running javascript after page is fully rendered 看一下这个问题: 页面完全呈现后运行javascript

Basically, it looks like you want to set a function to window.onload, and this will run after the page has been rendered. 基本上,您似乎想要将一个函数设置为window.onload,这将在呈现页面后运行。 Since I can't see your myapp.js, I don't know what the onReady function does there. 由于看不到您的myapp.js,因此我不知道onReady函数在做什么。 Whether or not you keep that line depends on what that function does. 是否保留该行取决于该函数的功能。 As Grampa says, you can place that line in the javascript file though. 正如Grampa所说,您可以将该行放置在javascript文件中。

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

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