When implementing client-server interactions through JavaScript, the operating system sometimes needs to be judged to achieve compatibility under different operating systems, such as: We have a Web site that works well under Windows XP, but under Ubuntu, because of a number of different features, Can cause subtle differences in browsing and even affect a good user experience. This time we need to use JavaScript to determine the type of operating system and some features, divide and conquer, so that the Web site in the Cross-platform browsing time to maintain a good user experience.
The following code implements the judgment of Windows, Mac, Linux, and UNIX scrubbing systems:
Copy Code code 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 we need more precise recognition of the Windows operating system, we can continue using the following code:
Copy Code code as follows:
<script type= "Text/javascript" language= "JavaScript" >
<!--
var Iswin = (Navigator.platform = = "Win32") | | (Navigator.platform = = "Windows"); Make sure that the Windows system
var isWin98 = isWin2000 = Iswinxp = false;
var suseragent = navigator.useragent;
if (Iswin) {
Iswin98=suseragent.indexof ("Win98") >-1 | | Suseragent.indexof ("Windows)" >-1; Win98
Iswin2000=suseragent.indexof ("Windows NT 5.0") >-1 | | Suseragent.indexof ("Windows C") >-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 is designed to implement the detection of whether the browser supports XML attributes:
Copy Code code 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 state is: ' +supportxml);
</script>
PS: In order to better avoid different browsers, different operating systems because the default text is different to the layout of the page, should try to avoid using fixed row height (height:12px;) to limit the height of the text, should try to use Height:auto, If you must limit the height of the text (such as only one row), you should use EM instead of PX (such as height:1.1em;), 1em=1 the height of the text, so that the text height changes with the size of the text, does not cause text truncation occurs.