从代码隐藏中的javascript函数检索值 [英] retrieve value from javascript function in codebehind

查看:40
本文介绍了从代码隐藏中的javascript函数检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在页面加载后的代码隐藏中从javascript函数中检索值。
javascript函数,如:

How can I retrieve value from javascript function in codebehind, on page load .. javascript function like :

<script type="text/javascript">
        function isIFrame() {
            var isInIFrame = (top.location != self.location);
            if (isInIFrame) {
                return "inside";
            }
            else {
                return "outside";
            }
        }
    </script>

和后面的代码,如:

protected void Page_Load(object sender, EventArgs e)
    {
        string resutOfExecuteJavaScript = "";
        // resutOfExecuteJavaScript = isIFrame(); // from javascript

        if (resutOfExecuteJavaScript == "inside")
        {
            // do something
        }
        else
        {
            // do something
        }
    }

谢谢。

推荐答案

您不能直接从服务器端代码调用客户端javascript方法。为此,您首先需要将函数结果分配给某个隐藏变量的值,然后在服务器端访问它

You cannot directly call a client side javascript method from server side code . For that first you need to assign the function result to value of some hidden variable and then access it in server side

假设您有一个像这样的隐藏字段

Suppose you have an hidden field like this

<input type="hidden" runat="server" id="hdnVal"/>

然后您可以将值设置如下

then you can set the value as below

document.getElementById("hdnVal").value=isIFrame();

然后在服务端

 string resutOfExecuteJavaScript = hdnVal.Value;

这篇关于从代码隐藏中的javascript函数检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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