пятница, 4 августа 2017 г.

Учим JavaScript. 38. Определяем Desktop или Mobile

index.html


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<script src="1.js" defer></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p id="out"></p>
</body>
</html>

1.js
// navigator.userAgent - заголовок user-agent, который браузер посылает серверу при запросе страницы.
document.querySelector('#out').innerHTML = navigator.userAgent;

console.log(navigator);

if(navigator.userAgent.match('iPhone') || navigator.userAgent.match('Android') || navigator.userAgent.match('iPad')) {
alert('Mobile');
}
else {
alert('Desktop');
}

1 комментарий: