javascript函数应该在每一个页面加载运行函数、加载、页面、在每一个

2023-09-10 19:05:22 作者:果味仙女℡

我在看,我保持一个asp.net 2的Web应用程序(但没有写)。

I am looking at an asp.net 2 web application that I am maintaining (but did not write).

有些事情应该发生在页面加载不这样做,但只是有时,似乎如果你使用的是Firefox 3的内部虚拟机的人。 jQuery和asp.net阿贾克斯被使用。

Some things that should happen on the page loading do not, but only sometimes, and it seems to be if you are using Firefox 3 inside a VM. JQuery and asp.net Ajax are used.

这应该每次运行(但是不)连接通过下面的JavaScript中的重要作用:

The important function that should run every time (but does not) is attached by the following Javascript:

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){ Sys.Application.add_load(ImportantFunction); });   
$(document).ready(function(){ Otherstuff(); });
$(document).ready(function(){ MoreStuff(); });
//]]>
</script>

但是,如果我用萤火虫来设置内部ImportantFunction()断点,所以不能打在页面加载在Firefox 3中,但打在一个Ajax更新。

But if I use firebug to set a breakpoint inside ImportantFunction(), it is not hit on page load in firefox 3, but it is hit on an ajax update.

在页面上有多个调用$(文件)。就绪,因为他们来自它背后的asp.net code不同部分。是的,他们做的所有执行。

In the page there are multiple calls to $(document).ready since they come from different parts of the asp.net code behind it. Yes, they do all execute.

推荐答案

试试这个:

<script type="text/javascript">
//<![CDATA[
$(document).ready(ImportantFunction);   
$(document).ready(Otherstuff);
$(document).ready(MoreStuff);
//]]>
</script>

把呼叫 Sys.Application.add_load ImportantFunction 的主体,即在你的.js文件

Put the call to Sys.Application.add_load in the body of ImportantFunction, i.e in your .js file:

function importantFunction()
{
   Sys.Application.add_load(ImportantFunction);
}

编辑:我不知道,如果它可以添加多种功能可在 $运行(文件)。就绪事件。如果你这样做可能有帮助,而不是:

I'm not sure if its possible to add multiple functions to be run on $(document).ready event. It might help if you did this instead:

<script type="text/javascript">
    //<![CDATA[
    $(document).ready(init);   
    //]]>
    </script>

而在的init 您可以包括调用所有的其它功能,例如:

And in init you can include calls to all the other functions, i.e:

function init()
{
   importantFunction();
   otherStuff();
   moreStuff();
   //Any other functions to be called upon page load go here
}

这也会使code更容易阅读:)

It will also make the code easier to read :)