Sys.Application.add_load() 与 $(document).ready() 与 pageLoad() [英] Sys.Application.add_load() vs. $(document).ready() vs. pageLoad()

查看:29
本文介绍了Sys.Application.add_load() 与 $(document).ready() 与 pageLoad()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面包含一些需要在页面加载时运行的 javascript.所述 javascript 需要定位 ServerControl 的客户端组件,它使用 $find() 来完成.

I have page that has some javascript that needs to run at page load. Said javascript needs to locate the client-side component of a ServerControl, which it does with $find().

当然,如果我将代码直接发送到页面上,它会在读取页面时执行,并且会失败,因为它所依赖的任何东西都尚未初始化.

Of course, if I emit my code directly onto the page, it executes as the page is being read, and fails because nothing it depends on is yet initialized.

如果我将代码放在 pageLoad() 函数中,它会运行得很好,因为 asp.net 会自动为任何名为 pageLoad() 的函数连接一个 onload 处理程序.问题是我真的不喜欢 pageLoad() 解决方案 - 主要是因为它是一个全局名称.如果我使用 pageLoad() 提交一些代码,我只知道其他一些程序员将在不合适的地方复制该方法,并且我们最终将得到一个包含两个或多个不同 pageLoad() 函数的页面,并且结果将是一堆神秘的错误,需要很长时间才能找到.

If I put my code inside a pageLoad() function, it runs just fine, because asp.net automatically wires up an onload handler for any function named pageLoad(). The problem is I really don't like the pageLoad() solution - mainly because it's a single global name. If I commit some code using pageLoad(), I just know that some other programmer is going to copy the approach, somewhere inappropriate, and we're going to end up with a page that includes two or more different pageLoad() functions, and the result will be a bunch of mysterious errors that will take forever to track down.

因此,我将我的代码放在传递给 jquery 的 $(document).ready() 的匿名函数中.这将失败,因为它在 ServerControl 的客户端组件存在之前运行.

So, I put my code inside an anonymous function passed to jquery's $(document).ready(). This fails, because it runs before the ServerControl's client-side component exists.

所以,我将我的代码放在一个由 Sys.Application.add_load() 传递的匿名函数中.这也会失败,因为 Sys 未定义.

So, I put my code inside an anonymous function passed to by Sys.Application.add_load(). This also fails, because Sys is undefined.

所以我最终决定将我的代码放在 Sys.Application.add_load() 中,然后将其放在 $(document).ready() 调用的函数中.这有效,但它的胃灼热程度几乎与 pageLoad() 一样.

So I finally settle on putting my code inside Sys.Application.add_load(), and then put that inside a function called by $(document).ready(). This works, but it gives nearly as much heartburn as pageLoad().

<script type="text/javascript">
    $(document).ready(function(){
        Sys.Application.add_load(function(){
            var component = $find(<clientid>);
            if (component) { <do something> }
        });
    });
</script>

必须有更好的方法来处理这个问题.

There has to be a better way of handling this.

有什么想法吗?

推荐答案

如果您可以控制代码隐藏,您可以通过以下方式注册 JavaScript 以在启动时运行:

If you have control over the code-behind, you can register the JavaScript to run at startup via something like:

this.Page.ClientScript.RegisterStartupScript(
    this.GetType(), 
    "StartupScript", 
    "Sys.Application.add_load(function() { functioncall(); });", 
    true);

只要您的组件已通过 Sys.Application.add_init() 加载,您就应该没问题...

As long as your component has been loaded via Sys.Application.add_init() you should be fine...

这篇关于Sys.Application.add_load() 与 $(document).ready() 与 pageLoad()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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