How to return a value from JavaScript to the pageload function

I call a JavaScript function in the page load

JScript File:

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:

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:

<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 call my javascript function here, I set the value of the hiddden field to "0" or "1", based on the type of browser, but when the page loads, the value of HhdnBrowsertype is always empty

Anyway, from javacript I return a value based on this value, I set my hidden field in the page load

Please help me on how I can return vlaue "0" or "1" from javscript to page load function

thank

+3
source share
2 answers

You are doing it wrong. For this, the server side has the following code:

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

IE ( ) "IE", Chrome "Chrome" ..

+1

,

Request.Browser.Browser

, IE

0

All Articles