执行JavaScript页​​面加载后, [英] Executing Javascript after page has loaded

查看:121
本文介绍了执行JavaScript页​​面加载后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的JavaScript,并有轻微问题搞清楚事件的时候火。这里的情况。我写的ASP.Net应用程序。在我的code后面,在Page_Load()我有一些code。我需要这个code首先执行。在此之后code已经执行完毕,那么我需要我的JavaScript执行。

有没有存在保证了code将在顺序执行的事件或某事?<​​/ P>

解决方案

最简单的方法是将你的函数调用添加到您的身体的onload事件

 &LT;身体的onload =yourFunction中()&GT;功能yourFunction中(){
}

另外,您也可以将一些脚本,在你的身体非常的结束即可。身体处理后,在这里的任何脚本将被执行。你可以做这样的事情。

  VAR的DivElement =的document.getElementById(DIVID);

没有它回来为空。

修改

我没有看到jQuery的标记你的问题,但是,如果你恰好在使用它,你也可以做到这一点:

  $(文件)。就绪(函数(){
    // DOM已准备就绪
});

或者更简单地说:

  $(函数(){
    // DOM已准备就绪
});

I am new to JavaScript and having a slight issue figuring out when events fire. Here is the situation. I am writing an ASP.Net application. In my code behind, in the Page_Load() I have some code. I need this code to execute first. After this code has finished executing, then I need my JavaScript to execute.

Is there an event or something that exists that insures the code will execute in that order?

解决方案

The simplest way would be to add your function call to your body's onload event

<body onload="yourFunction()">

function yourFunction() {
}

Alternatively, you could also place some script at the very end of your body. Any script in here will be executed after the body is processed. You'll be able to do things like

var divElement = document.getElementById("divId");

without it coming back as null.

EDIT

I didn't see jQuery tagged on your question, but, if you happen to be using it, you can also do this:

$(document).ready(function() {
    //dom is ready
});

or more simply:

$(function() {
    //dom is ready
});

这篇关于执行JavaScript页​​面加载后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆