简体   繁体   English

如何从 JavaScript 返回一个值到页面加载 function

[英]how to return a value from JavaScript to pageload function

I am calling my JavaScript function in page load我在页面加载中调用我的 JavaScript function

JScript File: JScript 文件:

function fnCheckBrowserType()
{
  if(navigator.appName == "Microsoft Internet Explorer" || navigator.appName == "Netscape")
  {
    //document.all["HhdnBrowsertype"].value=navigator.appName 
    document.all["HhdnBrowsertype"].value="1"
    alert(document.getElementById("HhdnBrowsertype").value); 
  }
  else
  {
    //document.getElementById("HhdnBrowsertype").value = navigator.appName 
    document.all["HhdnBrowsertype"].value="0"
    alert(document.getElementById("HhdnBrowsertype").value); 
  }
}

ASP.NET code behind: ASP.NET代码后面:

protected void Page_Init(object sender, EventArgs e)
{
  Page.ClientScript.RegisterStartupScript(typeof(string), "fnCheckBrowserType", "fnCheckBrowserType();", true);

  if (HhdnBrowsertype.Value.ToString() == "1")
  {
    int IE = 1;
  }
  else
  {
    int opera = 0;
  }
}

HTML: HTML:

<script src="Browsers.js" type="text/javascript"></script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <%--<input id="HhdnBrowsertype" type="hidden" name="HhdnBrowsertype" runat="server" />--%>
      <asp:HiddenField ID="HhdnBrowsertype" runat="server" />
    </div>
  </form>
</body>

In pageload i am calling my javascript function here i am setting the hiddden field value "0" or "1" based on the browser type but in page load HhdnBrowsertype value is always empty在页面加载中我调用我的 javascript function 在这里我根据浏览器类型设置隐藏字段值“0”或“1”但在页面加载中 HhdnBrowsertype 值始终为空

is there anyway from javacript i return an value based on that value i set my hidden field in page load无论如何从javacript我返回一个基于该值的值我在页面加载中设置我的隐藏字段

Please help in me how i can return an vlaue "0" or "1" from javscript to page load function请帮助我如何从 javscript 返回值“0”或“1”到页面加载 function

thanks谢谢

You're doing it the wrong way.. to do it server side have such code:你做错了..做服务器端有这样的代码:

protected void Page_Init(object sender, EventArgs e)
{
   string browser = Request.Browser.Browser;
   ...
}

For IE (all versions) it will return "IE", Chrome will return "Chrome" etc..对于 IE(所有版本),它将返回“IE”,Chrome 将返回“Chrome”等。

If you want to detect the browser type in code behide then use如果要在代码隐藏中检测浏览器类型,请使用

Request.Browser.Browser

this will give the browser type like IE这将给出像 IE 这样的浏览器类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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