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.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
How to write a browser-specific code using jQuery?
</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
</head>
<body style="text-align: center;">
<h1 style="color: green;">
GeeksforGeeks
</h1>
<h3>
How to write a browser-specific
code using jQuery?
</h3>
<div class="GFG"></div>
<script>
$(document).ready(function () {
if (navigator.userAgent.indexOf("Chrome") != -1) {
$(".GFG").text('Chrome Browser');
}
else if (navigator.userAgent.indexOf("Firefox") != -1) {
$(".GFG").text("Firefox Browser");
}
else if ((navigator.userAgent.indexOf("MSIE") != -1)
|| (!!document.documentMode == true)) {
alert('Internet Explorer');
}
else {
alert('Unknown Browser');
}
});
</script>
</body>
</html>
Output:

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.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
How to write a browser-specific code using jQuery?
</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
</head>
<body style="text-align: center;">
<h1 style="color: green;">
GeeksforGeeks
</h1>
<h3>
How to write a browser-specific
code using jQuery?
</h3>
<input type="button" id="btn" value="Click Here!">
<br><br>
<span class="GFG"></span>
<script>
$(document).ready(function () {
$("#btn").click(function () {
if (navigator.userAgent.indexOf("Chrome") != -1) {
$(".GFG").text('Chrome Browser').css({
"background-color": "green",
"color": "white",
"font-size": "26px",
"font-weight": "bold"
});
}
else if (navigator.userAgent.indexOf("Firefox") != -1) {
$(".GFG").text("Firefox Browser").css({
"background-color": "red",
"color": "white",
"font-size": "26px",
"font-weight": "bold"
});
}
else if ((navigator.userAgent.indexOf("MSIE") != -1)
|| (!!document.documentMode == true)) {
$(".GFG").text('Internet Explorer').css({
"background-color": "blue",
"color": "white",
"font-size": "26px",
"font-weight": "bold"
});
}
else {
$(".GFG").text('Unknown Browser').css({
"background-color": "yellow",
"color": "white",
"font-size": "26px",
"font-weight": "bold"
});
}
});
});
</script>
</body>
</html>
Output:

Similar Reads
How to Configure Mouse Wheel Speed Across Browsers using JavaScript ? The mouse wheel's scrolling speed varies with the choice of the web browser, even the DOM events and methods to change the scrolling speed are not the same. To provide zoom and animation on a web page, it is generally required to configure mouse speed. The speed of the wheel can be controlled by nor
3 min read
How to create a jQuery plugin with methods? Jquery is a Javascript library that is built to design or simplifies HTML, CSS, AJAX, DOM as well as event handling for web development. It is Open Source Software and free to all. More than 10 million websites use jquery. Jquery simply converts a line of javascript into methods that can be called i
2 min read
How to Build Simple Terminal like Website using jQuery ? Terminal is one of the most common tool used by developers. Many applications, frameworks, and even programming languages have more function which can be invoked using command line interface. Most of the computers come with at least one terminal in their operating system with command prompt now most
4 min read
How to detect the version of a browser ? This article includes the basic theory and technique of browser detection in JavaScript-enabled web browsers. Description: Even though most of the scripts work on JavaScript-enabled web browser, there are certain things that is not going to work on some browsers i.e. they are browser dependent and i
4 min read
How to run the code on change event using jQuery ? In this article, we will see how to run the code on change events using jQuery. The change event is used to trigger when the user changes the value of the element. Syntax: $(selector).change(function) Example: In the below example, we are creating a textarea element that contains GeeksforGeeks text.
1 min read