Use Javascript to determine the type of the operating system for compatibility under different operating systems

Source: Internet
Author: User

When using Javascript to implement interaction between the client and the server, you sometimes need to judge the operating system to achieve compatibility under different operating systems. For example, we have a website, in Windows XP, the browsing effect is good, but in Ubuntu, due to many different features, it may cause slight differences in browsing, and even affect the good user experience. At this time, we need to use Javascript to determine the type and some features of the operating system to achieve better user experience during cross-platform browsing.
The following code determines how to clean Windows, Mac, Linux, and Unix systems:
Copy codeThe Code is as follows:
<Script type = "text/javascript" language = "JavaScript">
<! --
Function check_ OS (){
Windows = (navigator. userAgent. indexOf ("Windows", 0 )! =-1 )? 1:0;
Mac = (navigator. userAgent. indexOf ("mac", 0 )! =-1 )? 1:0;
Linux = (navigator. userAgent. indexOf ("Linux", 0 )! =-1 )? 1:0;
Unix = (navigator. userAgent. indexOf ("X11", 0 )! =-1 )? 1:0;
If (windows) OS _type = "MS Windows ";
Else if (mac) OS _type = "Apple mac ";
Else if (linux) OS _type = "Lunix ";
Else if (unix) OS _type = "Unix ";
Return OS _type;
}
// -->
</Script>

If you need to perform more precise identification on Windows operating systems, you can continue to use the following code:
Copy codeThe Code is as follows:
<Script type = "text/javascript" language = "JavaScript">
<! --
Var isWin = (navigator. platform = "Win32") | (navigator. platform = "Windows"); // ensure that the system is windows
Var isWin98 = isWin2000 = isWinXP = false;
Var sUserAgent = navigator. userAgent;
If (isWin ){
IsWin98 = sUserAgent. indexOf ("Win98")>-1 | sUserAgent. indexOf ("Windows 98")>-1; // win98
IsWin2000 = sUserAgent. indexOf ("Windows NT 5.0")>-1 | sUserAgent. indexOf ("Windows 2000")>-1; // win2000
IsWinXP = sUserAgent. indexOf ("Windows NT 5.1")>-1 | sUserAgent. indexOf ("Windows Xp")>-1; // winxp
IsWin98 & alert ("window 98 ");
IsWin2000 & alert ("windows 2000 ");
IsWinXP & alert ("windows XP ");
}
// -->
</Script>

The following code checks whether the browser supports XML features:
Copy codeThe Code is as follows:
<Script type = "text/javascript" language = "JavaScript">
Var SupportXml = false;
Var xmldom;
If (window. ActiveXObject ){
Try {
Xmldom = new ActiveXObject ("Microsoft. XMLDOM ");
SupportXml = (xmldom. loadXML ("<OK/> "));
} Catch (e ){}
}
Else if (document. implementation & document. implementation. createDocument ){
SupportXml = true;
}
Alert ('xml status: '+ SupportXml );
</Script>

PS: in order to better avoid the impact of different browsers on the page layout of different operating systems due to different default texts, we should try to avoid the use of fixed line height (height: 12px;) in CSS ;) to limit the text height, use height: auto whenever possible. If you have to limit the text height (for example, to show only one row), use em instead of px (for example, height: 1.1em ;), 1em = 1 text height, so that the text height dynamically changes with the text size, does not cause text truncation.

Related Article

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.