创建公共Javascript函数 [英] Create public Javascript function

查看:88
本文介绍了创建公共Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我的想法是



我想创建一个公共JavaScript函数在用户控件中并从Web窗体中调用它。可能吗 ?我可以通过提供公共权限从Web窗体访问用户控件中的控件但无法访问javascript函数。

请任何人分享我的知识。



我的用户控件中的脚本可能是



Hello everyone,

My thinking is that

I want to create a public JavaScript function in User Control and call it from Web Form. Is it possible ? I can access controls in User Control from Web Form by giving public rights but can''t access javascript function.
please anyone share me knowledge.

Somehow my script in User Control may be

<script language="javascript" text="text/javascript">
function publicfn(){
alert("Public Function on User Control !");
}
</script>



这就是我想通过网络表单进行调用的方式




And this is how I want to make call from Web Form

<script type="text/javascript" language="javascript">
function callFn(){
var ctrlId=document.getElementById('<%= ctrl.ClientID %>');

alert(ctrlId.publicfn());

</script>





我想从User Control的公共js中获得一些计算结果通过在Web窗体上执行事件来实现。



谢谢大家。



I want to get some calculations from User Control''s public js function by doing event on Web Form.

Thanks guys.

推荐答案

默认情况下所有的javascript方法都是公开,直到你在关闭的帮助下私有化。您只需将其包含在您的页面中即可。您可以直接调用它,例如

By default all javascript method is public until you make it private with the help of closures. You just need to include it to your page.You can directly call it like
<script type="text/javascript" language="javascript">
function callFn(){
   publicfn();
} 
</script>



只需确保在渲染callFn之前渲染publicFn函数。 Javascript渲染序列很重要。如果callFn()函数先渲染而publicFn()函数渲染第二,那么它将不起作用。


Just make sure that publicFn function is rendered before render callFn. Javascript render sequence is important. If callFn() function render first and publicFn() function render second then it will not work.


这篇关于创建公共Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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