-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-javascript-method.html
75 lines (61 loc) · 1.92 KB
/
02-javascript-method.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>High Contrast Mode JavaScript method</title>
<style>
* {
font: normal 400 100%/1.4 sans-serif;
box-sizing: border-box;
}
</style>
<script src="jquery-3.1.1.slim.min.js"></script>
<script type="text/javascript">
function js_is_enabled(){
$("html").removeClass("no-js"),
$("html").addClass("js")
}
// Determines if document is in High Contrast Mode or not
// Returns boolean - true if high contrast, false otherwise
function HCTest(idval) {
var objDiv, objImage, strColor, strWidth, strReady;
var strImageID = idval; // ID of image on the page
// Create a test div
objDiv = document.createElement('div');
//Set its color style to something unusual
objDiv.style.color = 'rgb(31, 41, 59)';
// Attach to body so we can inspect it
document.body.appendChild(objDiv);
// Read computed color value
strColor = document.defaultView ? document.defaultView.getComputedStyle(objDiv, null).color : objDiv.currentStyle.color;
strColor = strColor.replace(/ /g, '');
// Delete the test DIV
document.body.removeChild(objDiv);
// Check if we get the color back that we set. If not, we're in
// high contrast mode.
if (strColor !== 'rgb(31,41,59)') {
return true;
} else {
return false;
}
}
$(document).ready(function(){
js_is_enabled();
var HCM = HCTest();
if (HCM === true) {
$('#message').text('TRUE'); // just for demo
$("body").addClass("hcm-on")
} else {
$('#message').text('FALSE'); // just for demo
$("body").removeClass("hcm-on")
}
});
</script>
</head>
<body>
<h1>Windows ® High Contrast Mode JavaScript detection</h1>
<p>This script is courtesy of ....</p>
<div id="message"></div>
</body>
</html>