Is. Net accessing the website a mobile phone or a PC?

Source: Internet
Author: User

I mainly looked at the http://topic.csdn.net/u/20100907/16/D01D667A-41AA-4A61-B985-8EB612E061AC.html here

The organization has a need to display different Web Pages Based on the devices that visit the website (mobile phones or computers.

I found it online. My summary is that it is best to use the useragent attribute of HTML Dom to determine what devices are accessing the website.

1. html page code:

<Divid = "useragent">
</Div>
<Scripttype = "text/JavaScript">
Document. getelementbyid ("useragent"). innerhtml = navigator. useragent;
// MessageBox. Show (this. webbrowser1.document. getelementbyid ("useragent"). innerhtml );
</SCRIPT>

In this way, the return value of useragent can be obtained.

2. Better understanding of useragent:

The following is an example of four programs.

(1) Check the visitor's browser and version number

<HTML>

<Body>

<SCRIPT type = "text/JavaScript">

VaR browser = navigator. appname
VaR B _version = navigator. appversion
VaR version = parsefloat (B _version)
Document. Write ("browser name:" + browser)
Document. Write ("<br/> ")
Document. Write ("browser version:" + version)

</SCRIPT>

</Body>
</Html>

(2) more information about the visitor's browser

<HTML>
<Body>

<SCRIPT type = "text/JavaScript">
Document. Write ("<p> Browser :")
Document. Write (navigator. appname + "</P> ")

Document. Write ("<p> browser version :")
Document. Write (navigator. appversion + "</P> ")

Document. Write ("<p> Code :")
Document. Write (navigator. appcodename + "</P> ")

Document. Write ("<p> platform :")
Document. Write (navigator. Platform + "</P> ")

Document. Write ("<p> cookies enabled :")
Document. Write (navigator. cookieenabled + "</P> ")

Document. Write ("<p> browser user proxy header :")
Document. Write (navigator. useragent + "</P> ")
</SCRIPT>

</Body>
</Html>

 

(3) All details about the visitor's browser

<HTML>
<Body>

<SCRIPT type = "text/JavaScript">
VaR x = navigator;
Document. Write ("codename =" + X. appcodename );
Document. Write ("<br/> ");
Document. Write ("minorversion =" + X. appminorversion );
Document. Write ("<br/> ");
Document. Write ("name =" + X. appname );
Document. Write ("<br/> ");
Document. Write ("version =" + X. appversion );
Document. Write ("<br/> ");
Document. Write ("cookieenabled =" + X. cookieenabled );
Document. Write ("<br/> ");
Document. Write ("cpuclass =" + X. cpuclass );
Document. Write ("<br/> ");
Document. Write ("online =" + X. Online );
Document. Write ("<br/> ");
Document. Write ("platform =" + X. Platform );
Document. Write ("<br/> ");
Document. Write ("UA =" + X. useragent );
Document. Write ("<br/> ");
Document. Write ("browserlanguage =" + X. browserlanguage );
Document. Write ("<br/> ");
Document. Write ("systemlanguage =" + X. systemlanguage );
Document. Write ("<br/> ");
Document. Write ("userlanguage =" + X. userlanguage );
</SCRIPT>

</Body>
</Html>

 

(4) Remind the user based on the browser

<HTML>
<Head>

<SCRIPT type = "text/JavaScript">
Function detectbrowser ()
{
VaR browser = navigator. appname
VaR B _version = navigator. appversion
VaR version = parsefloat (B _version)
If (Browser = "Netscape" | browser = "Microsoft internetexplorer") & (version> = 4 ))
{Alert ("your browser is already great! ")}
Else
{Alert ("your browser needs to be upgraded! ")}
}
</SCRIPT>

</Head>

<Body onload = "detectbrowser ()">
</Body>

</Html>

3. Five solutions without actual tests (for reference only)

(1) protected void page_load (objectsender, eventargs E)

{

Httpcapabilitiesbase r = (system. Web. configuration. httpcapabilitiesbase) request. browser;

String agent = (request. useragent + ""). tolower (). Trim ();

If (R. browser = "Mozilla ")

Response. Write ("recognizable: Mozilla ");

Else

Response. Write ("Unrecognized: Mozilla ");

If (R. ismobiledevice | r. browser = "unknown" | r. browser = "Mozilla ")

{

Response. Write ("Mobile Phone" + "\ r \ n" + R. browser + "\ r \ n" + R. type );

}

Else

{

Response. Write ("computer" + "\ r \ n" + R. browser + "\ r \ n" + R. type );

}

}

(2)
Protected void page_load (Object sender, eventargs E)
{
If (httpcontext. Current. Request. headers ["User-Agent"]! = NULL & httpcontext. Current. Request. headers ["User-Agent"]. tolower (). tostring (). indexof ("Mozilla ")! =-1)
{
// PC
}
Else
{
// Mobile phone
}
}

(3)

Publicstaticbool
Getclientweb ()
{

Bool result = false;
String clienttype = string. Concat (httpcontext. Current. Request. useragent );
If (clienttype. tolower (). Contains ("Mozilla") |
Clienttype. tolower (). Contains ("Opera "))
{
Result = true;
}
Return result;

}

(4)

Using system. Text. regularexpressions; // introduce the regular namespace in the header

// To improve accuracy and prevent WAP-supported browsers such as opera from being added to the operating system for verification. Openwave | later: PC OS

String ospat = "Mozilla | mshortate | winwap | openwave | Windows NT | Windows 3.1 | 95 | Blackcomb | 98 | Me | X Window | Longhorn | Ubuntu | AIX | Linux | amigaos | BEOs | HP-UX | OpenBSD | FreeBSD | NetBSD | OS/2 | osf1 | sun ";

String uagent = request. servervariables ["http_user_agent"];

RegEx Reg = new RegEx (ospat );

If (Reg. ismatch (uagent ))

{

Response. Write ("computer access ");

}

Else

{

Response. Write ("mobile access ");

}

Response. Write ("<br/>" + uagent );

(5)

Httpcapabilitiesbase r = (system. Web. configuration. httpcapabilitiesbase) request. browser;
String browser = R. browser. tostring (). Trim (). tolower ();
If (browser. Contains ("Firefox") | browser. Contains ("ie "))
{
// Response. Write ("computer" + "-- the browser is --" + browser); -- do nothing, continue

}
Else
{
// Response. Write ("Mobile Phone" + "-- browser is --" + browser );
// Jump to the mobile phone page
Response. Redirect ("http://www.jsdeai.com/index.html ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.