简体   繁体   中英

how to return a value from JavaScript to pageload function

I am calling my JavaScript function in 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 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

is there anyway from javacript i return an value based on that value i set my hidden field in page load

Please help in me how i can return an vlaue "0" or "1" from javscript to page load 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..

If you want to detect the browser type in code behide then use

Request.Browser.Browser

this will give the browser type like IE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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