Steven
Steven

Reputation: 31

How can I detect if a web page is opened by a mobile browser or an app?

if there is a web page which will be inserted into an app, it can be linked to another page within an app when I click somewhere on the web page because of the special link that can only be open within an app. However, if I want to make the same web page be opened within a mobile browser, I have to replace the special link.So,the prerequisite is that I need to detect if the web page is opened within an app or a mobile browser. Anyone can help?

Upvotes: 3

Views: 5498

Answers (3)

Juan Pablo
Juan Pablo

Reputation: 1223

1) Try to read the User Agent header from the http connections headers. It will show you the device that is connecting to your site. (proud to be hacked since header can be changed) Here you have a php example on how to read user agent

<?php  echo $_SERVER['HTTP_USER_AGENT']; ?>

2) Add some logic on your app and on your web page in order to "recognize" each other.

A simple logic could be to add a parameter read on your web page that must have an specific value in order to allow access. Your app must call the url with the expected value on it parameter.

Upvotes: 2

AHB
AHB

Reputation: 558

You can check the browser's useragent, which is a string. MDN

You can access it via Javascript and then perform conditionals based upon the result.

All browsers will return something slightly different so you will need to find something common to reduce the number of branches.

if(window.navigator.userAgent.match(/Android/i) {
    //Android browser, do something
}

Bunch of strings here: UserAgent strings

Upvotes: 0

Pheonix2105
Pheonix2105

Reputation: 1011

What you're looking for is the user-agent, each browser has a different user agent (name) which generally tells you what sort of device the browser is built for.

There are plenty of ways of retrieving the user agent with javascript check this previous StackOverflow question + answer here.

Upvotes: 0

Related Questions