VOOZH about

URL: https://www.geeksforgeeks.org/jquery/how-to-write-a-browser-specific-code-using-jquery/

โ‡ฑ How to write a browser-specific code using jQuery ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to write a browser-specific code using jQuery ?

Last Updated : 23 Mar, 2022

In this article, we will see how to write browser-specific code using jQuery. To write the browser-specific code, we will use the Navigator userAgent property and jQuery indexof() method. The Navigator userAgent property is used to get the user-agent headerโ€™s value sent to the server by the browser. It returns a string that represents values such as name, version, and platform of the browser.

The index() method is used to return the index of the specified elements with respect to the selector.

Syntax:

navigator.userAgent.indexOf(element)

Approach: First we will use navigator.userAgent.indexOf() method to get the index of browser. If the returned index does not -1 then print the browser name, otherwise test for the next browsers. 

Example 1: In this example, we will use navigator.userAgent.indexOf() method to print the browser name on the screen.

Output:

๐Ÿ‘ Image

Example 2: In this example, we will use navigator.userAgent.indexOf() method and a button, when the button is clicked, the function is called and print the browser name on the screen.

Output:

๐Ÿ‘ Image

Comment

Explore