go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  How to redirect request based on iphone, adroid or any other mobile device?
 
Subject: How to redirect request based on iphone, adroid or any other mobile device?
Author: WebSpider
Posted on: 02/07/2011 09:15:59 PM

With mobile devices like smart phone getting more and more popular, it's not uncommon that a traditional website which have been designed for desktop device is seeing a surge traffic from mobile devices. The layout of website contents which have been suit perfectly for the bigger desktop monitors is getting ugly to squeeze everything inside the tiny window. Sooner or later you, as a website administrator, need a way to serve customized layout or content based on the device where the request is sent from. This is usually done from server side, for example, JSP code:

http://www.forumeasy.com/forums/thread.jsp?tid=129712838927&fid=javaprof28

The code is very simple. All you have to do is to check User-Agent header that most browsers send along with each request. The header usually describes the specific type and version of the browser, togther with device information.

The same goal can be achieved from client side by embedded JavaScipt.

<script language="javascript">
	var user_agent = navigator.userAgent.toLowerCase();
	var is_iphone = (user_agent.indexOf('iphone') != -1);
	var is_android = ((user_agent.indexOf('android') != -1);
	var is_mobile = (user_agent.indexOf('mobile') != -1);
	if (is_mobile) { 
	    if(is_iphone){
               window.location = "URL_TO_IPHONE_CONTENT";
            }esle if(is_antroid){
               window.location = "URL_TO_ANDROID_CONTENT";
            }else{
               window.location = "URL_TO_MOBILE_CONTENT";
            }	
	}else{
            window.location = "URL_TO_DESKTOP_CONTENT";
	}
</script>	





References:

 


 
Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.