Developing

Introduction

Sometimes we need to know where the user came from, for example, if this is desktop then show button, or if this is a smart phone display this larger button.

Well, I have not found another way to do this without using javascript. So, here’s the code.

javascript

var mobile = function(){
	return {
		detect:function(){
			var uagent = navigator.userAgent.toLowerCase();
			var list = this.mobiles;
			var ismobile = false;
			for(var d=0;d<list.length;d+=1){
				if(uagent.indexOf(list[d])!=-1){
					ismobile = true;
				}
			}
			return ismobile;
		},
		mobiles:[
			"midp","240x320","blackberry","netfront","nokia","panasonic",
			"portalmmm","sharp","sie-","sonyericsson","symbian",
			"windows ce","benq","mda","mot-","opera mini",
			"philips","pocket pc","sagem","samsung","sda",
			"sgh-","vodafone","xda","palm","iphone",
			"ipod","android","ipad"
		]
	};
}();

if(mobile.detect()){
	alert('You are using a mobile phone to view this page..');
}else{
	alert('You are using a desktop browser to view this page..');
}

The code takes the useragent (a string that contains information about the browser) and compares with a list of mobile devices. If the useragent contains any of the mobile phones in the list of declared returns true, then you know it is a mobile phone or a desktop. Thus, you can redirect eg, the mobile user to a different page.

References

Ajax and Browser History – Detect mobile device / phone with javascript

David Walsh Blog – iPad Detection Using JavaScript or PHP

Also View

javascript – ReplaceAt

jQuery – Disable HTML Controls

jQuery – Dialog

jQuery – .ready

jwplayer – Show First Frame

jQuery – Themes List at Google Code

_____

Não se esqueça de deixar seu comentário, critica, elogio, dúvida e etc. Ele é muito importante para a melhoria do blog.
Do not forget to leave your comments, criticism, praise, questions, etc.. It is very important for improving the blog.

Advertisements

Comments on: "javascript – Detect Mobile" (4)

  1. […] javascript – Detect Mobile […]

  2. […] javascript – Detect Mobile […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: