0% found this document useful (0 votes)
18 views

Browser Detection Using Jquery

This document discusses using jQuery to detect the browser and conditionally execute code based on the browser. It also discusses calling a controller action from the client-side using AJAX to dynamically generate content on the client instead of from the server.

Uploaded by

drypz
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Browser Detection Using Jquery

This document discusses using jQuery to detect the browser and conditionally execute code based on the browser. It also discusses calling a controller action from the client-side using AJAX to dynamically generate content on the client instead of from the server.

Uploaded by

drypz
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

BROWSER DETECTION USING JQUERY Simple solution using client script <script type="text/javascript"> $(document).

ready(function () { //to get current version, use $.browser.version if ($.browser.msie) { //execute code specific for IE browser } else if ($.browser.mozilla) { //execute code specific for Mozilla browser } else if ($.browser.opera) { //execute code specific for Opera browser } else if ($.browser.webkit) { //execute code specific for Webkit browser, ex. google chrome } else { //code executed when other browser detected } }); </script>

Using MVC, we cannot execute client script from controller since server didnt know which client to target. Instead, you can call a controller action from client side. For example: [Controller] public ActionResult GetDynamicHTML() { var tb = new TagBuilder("P"); tb.InnerHtml = "This is a <strong>generated</strong> content."; var jsonContent = new { id = "someID", content = tb.ToString() }; return Json(jsonContent, JsonRequestBehavior.AllowGet); //return Content(tb.ToString()); } [Views] $("#btnGetContent").click(function () { $.ajax({ type: "GET", url: "/Home/GetDynamicHTML", //the URL of the controller action method data: null, // optional data success: function (result) { // do something with result ex. // if (result[id] == someID && $.browser.msie){ code here} else }, error: function (req, status, error) { alert('error'); // do something with error } }); });

You might also like