I have created a mobile website version of my desktop website. Now, on my desktop website I seek to redirect to the mobile only if the width of the screen is less than 680px while using a mobile device.
CODE:
$(document).ready(function () {
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
var deviceWidth = window.orientation == 0 ? window.screen.width : window.screen.height;
if (navigator.userAgent.indexOf('Android') >= 0 && window.devicePixelRatio) {
deviceWidth = deviceWidth / window.devicePixelRatio;
}
if (deviceWidth < 680) {
window.location.href = "Mobile page URL";
}
}
)};
The code is working perfectly, but the problem is that when the user enters the page from a mobile device, it loads some of the html content for a split second, and only afterwards redirects to the mobile page. How can I make sure that he is redirected before loading any page content? Am I missing anything? Should I create a 3rd blank page that is responsible for redirecting?
Thanks in advance for any light on that matter.