Webnerdoz
Webnerdoz

Reputation: 165

Javascript mobile redirect issue

Hi this has doing my head in for a couple of days now, I want to redirect to mobile websites for those checking the site from their devices with screen width of lower than 699. I am using this script for the redirection:

<script type="text/javascript">
<!--
if (screen.width <= 699) {
window.location = "http://www.mywebsite.com.au/mobile/";
}
//-->
</script>

The script is working fine when I am checking the site through firefox but not working in Dolphin browser..

It might be only my cell phone I have a Galaxy S2.

thanks in advance to anyone who can help me with this I really appreciate it!

New Update :-----ok this is getting very interesting. When I reduce the screen width to 599, the script works in the Dolpin browser.(screen.width <= 599) ---

Upvotes: 2

Views: 7107

Answers (3)

kiranvj
kiranvj

Reputation: 34107

I suggest something like this using User agent detection

var isMobile = function() {
   //console.log("Navigator: " + navigator.userAgent);
   return /(iphone|ipod|ipad|android|blackberry|windows ce|palm|symbian)/i.test(navigator.userAgent);
 };

Redirect like this

if(isMobile()) {
       window.location.href = "http://www.mywebsite.com.au/mobile/";
}

Upvotes: 7

user1903330
user1903330

Reputation:

My cell phone(MEIZU MX2) work fine. I don't know version of Dolphin browser. You can test 'screen.width'and'document.body.clientWidth'

I suggest you can write like this:

<script>     
    var userAgent = navigator.userAgent.toLowerCase();
    checkOS = function (r) {
        return r.test(userAgent);
    };
    var PlatformOS = {
        isWindows: checkOS(/windows nt|win32/),
        isMac: checkOS(/macintosh|mac os x/),
        isAndroidPad: checkOS(/nexus 7|xoom /),
        isAndroid: checkOS(/android/),
        isIphone: checkOS(/iphone/),
        isIpad: checkOS(/ipad/),
        isWindowsPhone: checkOS(/windows phone/),
        OS: "",
    }
    if (PlatformOS.isIpad || PlatformOS.isWindows || PlatformOS.isAndroidPad) {
        location.href = "http://www.mywebsite.com.au/pc/";
    }
    else if (PlatformOS.isIphone||PlatformOS.isWindowsPhone) {
        location.href = "http://www.mywebsite.com.au/mobile/";
    }

    window.onload = function () {            
        var currWidth = document.body.clientWidth;
        if (currWidth >= 699)
            location.href = "http://www.mywebsite.com.au/pc/";
        else
            location.href = "http://www.mywebsite.com.au/mobile/";
    }
</script>

Upvotes: 0

yoneal
yoneal

Reputation: 323

window.location is an object, so can you try the following:

window.location.replace("http://www.mywebsite.com.au/mobile/");

See window.location.replace

Upvotes: 0

Related Questions