Ik heb onderstaand script geschreven om stylesheets te updaten bij een tabwissel of met een knop.
Javascript is weer even geleden, en ik kreeg de knop niet aan de praat, omdat 'css' niet als global stond, maar als 'var css'. Vreselijk, ik vroeg me af of je aan een onclick iets kan vasthangen, zonder dat het global staat?
Javascript is weer even geleden, en ik kreeg de knop niet aan de praat, omdat 'css' niet als global stond, maar als 'var css'. Vreselijk, ik vroeg me af of je aan een onclick iets kan vasthangen, zonder dat het global staat?
JavaScript:
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
76
77
78
79
80
81
82
| function jsMessage(output) { var self = this; this.reset = function () { console.log('jsMessage.reset()'); clearTimeout(self.timeoutHide); clearInterval(self.intervalFade); clearTimeout(self.timeoutReset); clearTimeout(self.IntervalBlink); self.output.style.display = 'none'; self.output.style.opacity = 1; self.counter = 0; }; this.show = function(x) { self.reset(); self.output.innerHTML = x; self.output.style.display = 'block'; self.timeoutHide = setTimeout(self.hide,1500); self.IntervalBlink = setInterval(self.blink,200); }; this.blink = function() { if(self.counter%2 == 0) { self.output.style.display = 'none'; } else { self.output.style.display = 'block'; if(self.counter>4) clearTimeout(self.IntervalBlink); } self.counter++; } this.hide = function() { self.intervalFade = setInterval(self.fade,80); self.timeoutReset = setTimeout(self.reset,4000); } this.fade = function() { x = self.output.style.opacity; x = Math.max( Math.round(10*(10*x-1/(5*x)))/100 ,0); // x = Math.max((10*x-1)/10,0); console.log('x: '+x); self.output.style.opacity = x; if(x<=0) self.reset() }; this.output = output; this.reset(); } function Css(topAlert) { var self = this; this.Reload = function() { topAlert.show('StyleSheets are being updated'); for (var link of document.querySelectorAll("link[rel=stylesheet]")) link.href = link.href.replace(/\?.*|$/, "?ts=" + new Date().getTime()); topAlert.show('StyleSheets are updated on ' + new Date().toLocaleString()); } } window.addEventListener('DOMContentLoaded', initial, true); function initial() { var topAlert = new jsMessage(document.getElementById("topAlert")); css = new Css(topAlert); // global console.log('window - DOMContentLoaded'); window.addEventListener("focus", css.Reload, false); window.addEventListener("blur", function(event) { }, false); } </script> </head> <body> <div style="overflow:hidden;width:100%;position:fixed;background-color: #fff1ae;border-bottom: 1px solid #999;"> <button style="float:left" onclick='css.Reload()'>Css.Reload()</button> <div style="float:left" id='topAlert' >No message to display</div> </div> |