Web sitenizde mobil ve masaüstü için iki ayrı tasarım yaptıysanız mobil tarayıcı kontrolünü javascriptte navigotor.useragent fonksiyonu ile yapabilirsiniz. navigator.useragent bize tarayıcı bilgisini vermektedir.
Örneğin bir x değişkenine bu değeri aktaralım.
var x = "User-agent bilgisi: " + navigator.userAgent;
Bunu yazdırmaya kalktığımızda
User-agent bilgisi: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
gibi bir sonuçla karşılaşırız. Buradaki bilgiyi match ederek tarayıcının mobil olup olmadığını kontrol edip istediğimiz bir adrese yönlendirebiliriz. .
Kullanacağımız fonksiyon aşağıdadır.
function mobilkontrol() { if( navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/Windows Phone/i) ){ return true; } else { return false; } }
Fonksiyonumuz bu şekilde. Kontrolünü de şu kodlarla yapabiliriz.
if (mobilkontrol()==true){ window.location = "http://m.siteadresi.com/"; }
Burada mobilkontrol fonksiyonu geriye true değer dönderdiğinde mobil adresimize yönlendirme yapıyoruz. Sitenizin adresine göre tırnak içerisindeki içeriği değiştirebilirsiniz.
Html sayfanın tamamı aşağıdadır.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>tarayıcı kontrolü</title> <script type="text/javascript"> function mobilkontrol() { if( navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/Windows Phone/i) ){ return true; } else { return false; } }
if (mobilkontrol()==true){ window.location = "http://m.siteadresi.com/"; } </script> </head>
<body> Tarayıcı mobil değil. </body> </html>
Yorumlar.
bir türlü çalıştıramadım
Eline sağlık dostum çok işime yaradı
Tesekkurler. Iyi calismalar