如何从后台代码执行javascript函数,但仅在页面加载后执行 [英] How to execute javascript function from code-behind but only after the page loads

查看:152
本文介绍了如何从后台代码执行javascript函数,但仅在页面加载后执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了其他有关如何从后台代码执行javascript函数的解决方案.问题在于它会在页面加载之前触发,因此我无法为其添加逻辑.

I've found other solution on how to executes a javascript function from code-behind. The problem is that it firing before the page loads, so I can't add logic to it.

if(result)
{
   Page.ClientScript.RegisterClientScriptBlock(typeof(Page), 
                                               "Script", "confirmPwdChange();", true);
}

在客户端

<script>
   confirmPwdChange()
   {
     alert("Password has been successfully changed.");
     //Add more logic here, after user clicks the OK button...
   }
</script>

没有Jquery,如果可能的话.

No Jquery, if possible.

感谢您的帮助.

推荐答案

您可能希望在脚本文件中包含window.onload.像这样的东西.

You might want to include the window.onload in your script file. something like this..

StringBuilder script = new StringBuilder();
script.Append("var existingHandler = window.onload;");
script.Append("window.document.body.onload = function(){ confirmPwdChange(); if (existingHandler){ existingHandler()}}");
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), 
                                           "Script", script.ToString(), true);

这确保您的更改密码在页面加载时被调用,并保留页面上已经声明的任何其他现有处理程序.

this ensure, your change password is called on page load and retains any other existing handlers declared already on the page.

这篇关于如何从后台代码执行javascript函数,但仅在页面加载后执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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