2 * COMMON DHTML FUNCTIONS
3 * These are handy functions I use all the time.
5 * By Seth Banks (webmaster at subimage dot com)
6 * http://www.subimage.com/
8 * Up to date code can be found at http://www.subimage.com/dhtml/
10 * This code is free for you to use anywhere, just keep this comment block.
14 * X-browser event handler attachment and detachment
15 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
17 * @argument obj - the object to attach event to
18 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
19 * @argument fn - function to call
21 function addEvent(obj, evType, fn){
22 if (obj.addEventListener){
23 obj.addEventListener(evType, fn, false);
25 } else if (obj.attachEvent){
26 var r = obj.attachEvent("on"+evType, fn);
32 function removeEvent(obj, evType, fn, useCapture){
33 if (obj.removeEventListener){
34 obj.removeEventListener(evType, fn, useCapture);
36 } else if (obj.detachEvent){
37 var r = obj.detachEvent("on"+evType, fn);
40 alert("Handler could not be removed");
45 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
47 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
49 * Gets the full width/height because it's different for most browsers.
51 function getViewportHeight() {
52 if (window.innerHeight!=window.undefined) return window.innerHeight;
53 if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
54 if (document.body) return document.body.clientHeight;
56 return window.undefined;
58 function getViewportWidth() {
61 if (window.innerWidth!=window.undefined) return window.innerWidth;
62 if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
63 if (document.body) return document.body.clientWidth;
67 * Gets the real scroll top
69 function getScrollTop() {
70 if (self.pageYOffset) // all except Explorer
72 return self.pageYOffset;
74 else if (document.documentElement && document.documentElement.scrollTop)
77 return document.documentElement.scrollTop;
79 else if (document.body) // all other Explorers
81 return document.body.scrollTop;
84 function getScrollLeft() {
85 if (self.pageXOffset) // all except Explorer
87 return self.pageXOffset;
89 else if (document.documentElement && document.documentElement.scrollLeft)
92 return document.documentElement.scrollLeft;
94 else if (document.body) // all other Explorers
96 return document.body.scrollLeft;