JavaScript Detect Mobile Browser Type

Share this article

JavaScript code snippet to detect if a user is viewing the website using a mobile device and what mobile browser they are using. It’s helpful to detect browser version as there are hundreds of mobile devices out there but not as many browsers. Here is a JavaScript function that may help you.

// Browser Detection Javascript
function whichBrs() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1) return 'Opera';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("webtv") != -1) return 'WebTV';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'Internet Explorer';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('/') != -1) {
		if (agt.substr(0,agt.indexOf('/')) != 'mozilla') {
			return navigator.userAgent.substr(0,agt.indexOf('/'));}
		else return 'Netscape';} else if (agt.indexOf(' ') != -1)
	return navigator.userAgent.substr(0,agt.indexOf(' '));
	else return navigator.userAgent;
}

Frequently Asked Questions (FAQs) about Detecting Mobile Browsers

What is the importance of detecting a mobile browser?

Detecting a mobile browser is crucial for web developers to ensure that their websites or web applications are responsive and provide an optimal user experience across different devices. By detecting a mobile browser, developers can tailor the content, layout, and functionality to suit the capabilities and constraints of mobile devices. This can include simplifying the navigation, reducing the amount of data loaded, increasing the size of touch targets, and more.

How does JavaScript help in detecting a mobile browser?

JavaScript provides several methods to detect a mobile browser. One common approach is to use the navigator.userAgent property, which returns a string representing the browser’s user-agent header. This string can be tested against known mobile browser user-agent strings to determine if the browser is a mobile browser. Another approach is to use the window.orientation property, which is typically only present on mobile devices.

Can I detect a mobile browser using CSS?

Yes, CSS media queries can be used to detect a mobile browser based on the viewport size. However, this method is not as reliable as JavaScript methods because it depends on the screen size rather than the actual device or browser type. It’s best used for making layout adjustments rather than altering functionality based on device type.

What are some limitations of detecting a mobile browser?

While detecting a mobile browser can be useful, it’s not foolproof. User-agent strings can be spoofed or altered, and new mobile browsers may not be recognized by existing detection scripts. Additionally, some devices, like tablets or large smartphones, may blur the line between mobile and desktop browsers.

How can I test my mobile browser detection code?

You can test your mobile browser detection code by using device emulation features in browser developer tools, such as Chrome DevTools or Firefox Developer Tools. These tools allow you to simulate different devices and screen sizes, and even spoof user-agent strings.

Can I detect specific mobile browsers, like Safari or Chrome?

Yes, the user-agent string returned by navigator.userAgent includes information about the specific browser being used, so you can test this string against known user-agent strings for specific mobile browsers.

How can I handle unknown or new mobile browsers?

To handle unknown or new mobile browsers, you can use a combination of feature detection and progressive enhancement. Feature detection involves testing for the presence of specific features or APIs, while progressive enhancement involves building your site or application to work with a basic level of functionality on all browsers, then adding enhancements for browsers that support them.

Is it possible to detect a mobile browser on the server side?

Yes, server-side languages like PHP or .NET can also detect mobile browsers by examining the user-agent string sent in the HTTP request header. However, this method may not be as accurate or up-to-date as client-side detection using JavaScript.

What is the role of regular expressions in detecting a mobile browser?

Regular expressions play a crucial role in detecting a mobile browser. They are used to match the user-agent string against a pattern of known mobile browser user-agent strings. This allows the script to identify whether the user is accessing the site from a mobile device or not.

Can I use a library or plugin to detect a mobile browser?

Yes, there are several libraries and plugins available that can simplify the process of detecting a mobile browser. These tools typically provide a more comprehensive and up-to-date list of mobile browser user-agent strings, and may also include additional features like device capability detection. However, they also add an extra dependency to your project and may be overkill for simple use cases.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

javascript detect mobile browserjQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form