Javascript函數應該在每個頁面加載時運行

[英]Javascript function should be running on every page load


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

我正在查看我正在維護的asp.net 2 Web應用程序(但沒有寫)。

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.

在頁面加載時應該發生的一些事情不會,但只是有時,似乎是你在VM中使用Firefox 3。使用JQuery和asp.net Ajax。

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

應該每次都運行的重要函數(但不是)通過以下Javascript附加:

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

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.

但是,如果我使用firebug在ImportantFunction()中設置一個斷點,它在firefox 3中的頁面加載時不會被擊中,但它會在ajax更新中被擊中。

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.

在頁面中有多次調用$(document).ready,因為它們來自asp.net代碼的不同部分。是的,他們都會執行。

4 个解决方案

#1


Try this:

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

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

將調用Sys.Application.add_load放在ImportantFunction的主體中,即在.js文件中:

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

Edit: 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:

編輯:我不確定是否可以在$(document).ready事件上添加多個函數。如果您這樣做可能會有所幫助:

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

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

在init中,您可以包含對所有其他函數的調用,即:

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

It will also make the code easier to read :)

它還將使代碼更容易閱讀:)

#2


Is there any reason why you can't use the ASP.NET AJAX pageLoad function instead of $(document).ready()?

是否有任何理由不能使用ASP.NET AJAX pageLoad函數而不是$(document).ready()?

function pageLoad(sender, args)
{
     ImportantFunction();
     OtherStuff();
     MoreStuff();
}

This is part of the ASP.NET AJAX client page lifecycle and all JavaScript code inside will be executed on every page load, including asynchronous postbacks.

這是ASP.NET AJAX客戶端頁面生命周期的一部分,內部的所有JavaScript代碼都將在每個頁面加載時執行,包括異步回發。

#3


You're using jQuery to attach a "load" method which then in-turn attaches a load event and I think this is where your problem is.

你正在使用jQuery附加一個“加載”方法,然后反過來附加一個加載事件,我認為這是你的問題所在。

$(document).ready and Sys.Application.add_load are pretty much the same, or so my understanding goes. I'm not really sure in which order the browser will execute them though.

$(document).ready和Sys.Application.add_load幾乎相同,或者我的理解是這樣。我不確定瀏覽器執行它們的順序。

I'd suggest removing the Sys.Application.add_load wrapper call on ImportantFunction so you're not trying to attach to an event stack that has already fired.

我建議刪除ImportantFunction上的Sys.Application.add_load包裝器調用,這樣你就不會嘗試連接到已經觸發的事件堆棧。

#4


According to this recent blog posting, this is a bug (or at least a misfeature) in FireFox 3. He suggests naming your important function PageLoad to get it to work cross-browser, though I'm not sure if that will work or not.

根據最近的博客文章,這是FireFox 3中的一個錯誤(或至少是一個錯誤)。他建議命名你的重要功能PageLoad讓它跨瀏覽器工作,雖然我不確定這是否會起作用。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/01/06/72f15f9977cd9522613f8ef0c0f3fc62.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com