JavaScript to detect whether page is load on Mobile or Desktop

 The web is available on numerous gadgets, from Desktops to Smartphones and wearable. Some of you may be in any event, perusing this site on your cell phones. With respect to the engineers, it is on them how they make any site open and responsive independent of the gadget utilized. 

Yet, how would you separate between a cell phone and a personal computer? CSS media inquiries is a decent theory, yet it's simply restricted to styling a website page. What might be said about usefulness? Javascript? Right estimate. 

We can utilize Javascript to tell whether the site is being seen from a Smartphone gadget or personal computer. Here's a straightforward content that does this.

JavaScript code with HTML:

<!DOCTYPE html>
<html>
<head>
<title>Mobile Test</title>
</head>
<body>
<p id="text"></p>
<script type="text/javascript">
var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
var element = document.getElementById('text');
if (isMobile) {
   element.innerHTML = "You are using Mobile";
} else {
element.innerHTML = "You are using Desktop";
}
</script>
</body>
</html>

Here, we are composing the content in the html document to keep it brief. 

In the first place, we are proclaiming an isMobile variable that tests the navigator.userAgent against an ordinary articulation. The navigator.userAgent object strategy gives the program subtleties in plain content configuration. The ordinary articulation is just checking the event of the example through the test work. 
In the event that it returns valid, which implies it is passes the versatile check ordinary articulation and subsequently is a cell phone. Else we print the work area string.

Post a Comment

0 Comments