Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hi i have an user control suppose a.ascx, i want to write a javascript file in user control code behind file. Can any one please convert the below javascript file to call from code behind.

Javascript file

JavaScript
function xrxIdentifyCurrentBrowser() {
       var uaString = navigator.userAgent;
       return (((uaString != undefined) && (uaString != null)) ? (((uaString = uaString.toLowerCase()).indexOf("galio") >= 0) ?
           "FirstGenBrowser" : ((uaString.indexOf("webkit") >= 0) ? "SecondGenBrowser" : "Unknown")) : "No User Agent");
   }
Posted

I also tried to help you out on that Stack Overflow thread of your question, the problem actually is that you shouldn't actually ask the server-side code to take over the client-side code too. If you have to then you have to register the code at the start-up and then execute it.

But, another solution, which I provided there too, is that you need to determine the browser only. Then you can make a good use the Request.Browser object, to check the properties of the browser. It would give you details about the browser itself, and can be used in the server-side C# code.

Otherwise, the JavaScript code as Solution 1 says, must be executed in the client-side code environment (by JavaScript only). You can make up a file, and reference that file in your actual HTML DOM, or you can write that in a script element in your HTML DOM. But still it won't be as much efficient as it can be to perform all these checks at the server-side using that Request.Browser object.

You can read the MSDN document[^] about the Request.Browser and different members it exposes for you to work out in determining the browsers.

How to: Detect Browser Types and Browser Capabilities in ASP.NET "Web Forms (Same underlying code; don't panic)"[^]
 
Share this answer
 
Comments
Ashad25 16-Feb-15 14:58pm    
Hi Afzaal thanks for ur post. As i told before i need to check the browser engine not browser type. So from the Request.Browser it is not possible. I need to check if the Browser engine is WebKit then i want to execute some codes in code behind.
Afzaal Ahmad Zeeshan 16-Feb-15 15:02pm    
That answer has been given there too. Moderizr, and some other libararies would be great for this. You can also use the CSS3 media queries to determine the screen sizes and other aspects of them. Why actually do you need to check the engine? When you can control them using some bootstrap, twitter bootstrap can be good one.
I believe that code-behind files can only contain server side code in C# or VB.NET. You have 2 options:

  1. Embed your function into your page like this:
    HTML
    <script type="text/javascript">
    function xrxIdentifyCurrentBrowser() {
      var uaString = navigator.userAgent;
      return (((uaString != undefined) && (uaString != null)) ? (((uaString = uaString.toLowerCase()).indexOf("galio") >= 0) ?
                "FirstGenBrowser" : ((uaString.indexOf("webkit") >= 0) ? "SecondGenBrowser" : "Unknown")) : "No User Agent");
        }
    </script>
  2. Create a JS file containing your functions somewhere inside your site folder and insert it into your page like this
    HTML
    <script type="text/javascript" src="yourfile.js"></script>
 
Share this answer
 
Comments
Ashad25 16-Feb-15 14:19pm    
In the second option if u register the javascript file in your ascx page, then how you will call the method in code behind file.
Vladimir Svyatski 16-Feb-15 14:40pm    
That's not possible I think. But as I can see you're trying to detect your browser, from the code behind you should use this. Especially you're interested in the Browser property.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900