go to  ForumEasy.com   
JavaPro  
 
 
   Home  |  MyForum  |  FAQ  |  Archive    You are not logged in. [Login] or [Register]  
Forum Home » JavaScript » How to redirect request based on iphone, adroid or any other mobile device?
Email To Friend  |   Set Alert To This Topic Rewarding Points Availabe: 0 (What's this) New Topic  |   Post Reply
Author Topic: How to redirect request based on iphone, adroid or any other mobile device?
WebSpider
member
offline   
 
posts: 147
joined: 06/29/2006
from: Seattle, WA
  posted on: 02/07/2011 09:15:59 PM    Edit  |   Quote  |   Report 
How to redirect request based on iphone, adroid or any other mobile device?
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>	


 Profile | Reply Points Earned: 0

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