6

We have developed a website which has general homepage and mobile page, one is for mobile and other is for PC.

I want code to detect the device automatically and navigate to the mobile homepage if the user is from mobile and navigate to the homepage(general) if the user is from PC and problem is it should be using JavaScript or plain HTML.

3

4 Answers 4

7

http://detectmobilebrowsers.com/ has an open source javascript function to detect a mobile browser that you can use.

Sign up to request clarification or add additional context in comments.

1 Comment

do you know of any examples about using it? I looked at the website and page source code. It is so unclear how to use its javascript version. Thanks!
2

To determine this you need to check the HTTP header for the request for your website, in particular the User-Agent string.

You haven't mentioned what technologies you're using, but you can redirect at the web-server level or within your application.

2 Comments

From your link: User agent sniffing is now considered poor practice, ... Instead, the W3C recommends creating HTML markup that is standard, allowing correct rendering in as many browsers as possible, and to test for specific browser features rather than particular browser versions or brands.
@Sablefoste - Agree feature 'sniffing' is now considered better practice. I did write the answer 4 years ago :-)
2

Don't make separate sites. Use reactive design. The browser on my phone is just as good as the one on my desktop.

1 Comment

Reactive design is a bit vague in this context. It can be useful to conditionally navigate to a separate page where mobile can be assumed allowing for removal of bloated elements in the server response like heavy images, scripts, and stylesheets. Or where the context of use for mobile is drastically different from desktop. Simply serving a one page reactive document is not always the best approach, as addressed in Ethan Marcotte's Becoming Responsive chapter in Responsive Web Design (from A Book Apart).
0

you can use below code,

var index = navigator.appVersion.indexOf("Mobile"); 

This will return you negative value if the request is not from mobile site.

Happy learning :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.