简体   繁体   中英

document.ready sometimes doesn't execute

I've run into a situation where, occasionally, it seems the $(document).ready() doesn't fire on a page, which means key events don't get wired up so the page doesn't work right. It works most of the time but every now and then it doesn't and I'm uncertain how to fix it so I'm appealing to the great pool of knowledge found here. What can I do to ensure that my initialization function that is called when the document is ready is always executed? Or why does document.ready not always get triggered.

I implemented a tabs control on the index.aspx page:

    <script type="text/javascript">
    $(document).ready(function () {
        $("#tabs").tabs();
    });

</script>

Here is the HTML:

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<div>&nbsp;</div>
<div id="navLinks" style="display: inline-block">
    <a href="ImportAssets.aspx">Import Assets</a>
    <a href="ImportUsers.aspx">Import Users</a>
</div>
<div>
    <div>&nbsp;</div>
    <div id="tabs">
        <div>
            <ul>
                <li><a href="Search.aspx">Search</a></li>
                <li><a href="Users.aspx">Users</a></li>
                <li><a href="Assets.aspx">Assets</a></li>
                <li><a href="Organizations.aspx">Organizations</a></li>
            </ul>
        </div>
    </div>

Each aspx page has its own document.ready, for example, the Users.aspx:

<script type="text/javascript">

    $(document).ready(function () {
        initializeUsersPage();
    });
</script>

The problem that I have is that, very occasionally, the initializeUsersPage() function doesn't get executed when transitioning from another tab to the Users tab. Why? And how do I solve that?

Thanks in advance.\\, Darwin

In ASP.NET Web Form, we normally use pageLoad , if we need to find a control when using with ASP.NET Ajax such as UpdatePanel .

However, I could not say for sure that it could solve your problem, but you could at least give a try.

<script type="text/javascript">
   //<![CDATA[     
   function pageLoad() {
      var listBox = $find("<%= SelectedTagRadListBox.ClientID %>");
   }            
   //]]>
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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