www.webdeveloper.com
Results 1 to 2 of 2

Thread: mobile script

Hybrid View

  1. #1
    Join Date
    Sep 2006
    Posts
    375

    mobile script

    Hi

    Say someone has a webpage designed for desktops and decides they want the webpage to work properly for mobile devices. They want the desktop webpage to be left alone.

    What is the script or method to detect a mobile webpage and load a new html/css for tablet/phone size . users will go to the same webpage as the desktop location and be redirected to a mobile version if need be.

    This avoids the headache of making a static desktop webpage responsive.

  2. #2
    Join Date
    May 2013
    Posts
    15
    This is my first post here if I'm doing something wrong by sharing this snippet code, Moderator can delete this post and let me know Thanks

    Add the following script on the head section of each page of the desktop website:
    Code:
    var _mobile_url = "http://yourdomain.com/mobilepath"; //Here Just add the path to your mobile content
    var _screen_width = 800; // Here you specify the width of the devices you want to switch to the mobile website
    
    detect_mobile(_mobile_url, _screen_width, true);
    
    function detect_mobile(_mobile_url, _screen_width, _handle_android) {
    	if (document.referrer.indexOf(_mobile_url) == -1) {
    		if(!mobileSwitcher_readCookie("mobileSwitcher_skipredirect") && (screen.width < _screen_width || (_handle_android && navigator.userAgent.match(/Android/i))) ) {
    			location.replace(_mobile_url);
    		}
    	} else {
    		mobileSwitcher_createCookie("mobileSwitcher_skipredirect", "true", 1);
    	}
    }
    
    function mobileSwitcher_createCookie(name,value,days)
    {
    	if (days)
    	{
    		var date = new Date();
    		/*
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		*/
    		date.setTime(date.getTime()+(1*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}
    	else var expires = "";
    	document.cookie = name+"="+escape(value)+expires+"; path=/";
    }
    function mobileSwitcher_readCookie(name)
    {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0; i < ca.length; i++)
    	{
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
    	}
    	return null;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles