Reputation: 313
On our index page we have script to redirect users who are on smartphones (iPhone, Android & Windows Phone) to our mobile site. The technique we use is:
if ( navigator.userAgent.match( /iPhone/ ) && ! navigator.userAgent.match( /iPad/ ) ) {
window.location="mobile.html";
}
else if ( navigator.userAgent.match( /Android/ ) && ! navigator.userAgent.match( /Android 3/) ) {
window.location="mobile.html";
}
else if ( navigator.userAgent.match( /Windows Phone/ ) || navigator.userAgent.match( /Zune/ ) ) {
window.location="mobile.html";
}
Everything was working perfectly until we tested this on IE9 which for some reason redirects to the mobile site, even though its userAgent does not contain any of the strings above. The userAgent for IE9 is:
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
IE8 does not behave this way nor does any other platform. Is the script incorrect or did IE strike down again with its vengeful douchebaggery? Thanks.
Upvotes: 2
Views: 684
Reputation: 1
Thanks for your answer and help. The following worked for me using a Windows 8 Phone
if(/iPhone/.test(navigator.userAgent) && !/iPad/.test(navigator.userAgent)){
window.location="/mobile";
}else if(/Android/.test(navigator.userAgent) && !/Android 3/.test(navigator.userAgent){
window.location="/mobile";
}else if(/Windows Phone/.test(navigator.userAgent) || /Zune/.test(navigator.userAgent)){
window.location="/mobile";
}
I changed the window.location="mobile.html" to the actual directory I created for the mobile's index.html file
ex. /root directory/mobile
Upvotes: 0
Reputation: 313
According to this post regarding IE9 User Agents is that IE9 changes its User Agent when in compatibility mode and can contain the 'Zune' string. Looks like we'll have to try and use another string to redirect Windows Phone users. Thanks.
Upvotes: 2
Reputation: 11342
When you use the .match() method, it returns array, which can be empty([]
) or not.
IE is probrably considering that []
is a true
expression, so I suggest you to do the following:
if(/iPhone/.test(navigator.userAgent) && !/iPad/.test(navigator.userAgent)){
window.location="mobile.html";
}else if(/Android/.test(navigator.userAgent) && !/Android 3/.test(navigator.userAgent){
window.location="mobile.html";
}else if(/Windows Phone/.test(navigator.userAgent) || /Zune/.test(navigator.userAgent)){
window.location="mobile.html";
}
I guess it's gonna work because the .test() method will only return true
or false
Upvotes: 1