Articles :: JavaScript :: Common JavaScript Library
written by Toby Miller on December 1, 2006 December 1, 2006 This is the Common JavaScript Library that I tend to use on all of my projects. Feel free to pilfer.
source code:
1:/**
2: * A common library of functions used in various javascript libraries that I've
3: * written.
4: *
5: * @author Toby Miller
6: * @copyright Copyright (C) 2005-2006, Toby Miller
7: * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
8: */
9:
10:/*
11: * DO NOT EDIT BELOW THIS LINE
12: */
13:
14:/**
15: * add an event to an element
16: *
17: * @param object dom element to add event to
18: * @param string w3c-standard event name being handled
19: * @param object w3c-standard event object or simulated function
20: * @param boolean [optional] whether to truly process the onload event or not (default = false)
21: * @return void
22: */
23:function addEvent(element, eventName, eventObject)
24:{
25: if (element.addEventListener)
26: {
27: // this is a w3c-compliant browser, just wrap existing functions
28: element.addEventListener(eventName, eventObject, false);
29: }
30: else if (element.attachEvent)
31: {
32: // collect optional parameters
33: var processOnload = (arguments.length == 4) ? arguments[3] : false;
34:
35: if ((element == window) && (eventName == 'load') && (!processOnload))
36: {
37: // store this event, we'll do it in order with the other onloads
38: _onloads.push(eventObject);
39: }
40: else
41: {
42: // add the new event
43: element.attachEvent('on' + eventName, eventObject);
44: }
45: }
46: return(true);
47:}
48:_onloads = [];
49:addEvent(window, 'load', function(){for(var i = 0; i < _onloads.length; i++){_onloads[i]();}}, true);
50:
51:/**
52: * gets information pertinent to the page dimensions
53: * screenheight viewable screen height
54: * screenwidth viewable screen width
55: * pageheight full page height
56: * pagewidth full page width
57: *
58: * @param event
59: * @return void
60: */
61:var screenheight = 0;
62:var screenwidth = 0;
63:var pageheight = 0;
64:var pagewidth = 0;
65:var centerx = 0;
66:var centery = 0;
67:function pageinfo()
68:{
69: var dombody = document.body;
70:
71: if (document.documentElement)
72: {
73: if ((document.body.clientHeight == document.body.offsetHeight) && (document.body.offsetHeight == document.body.scrollHeight))
74: {
75: dombody = document.documentElement;
76: }
77: if ((document.body.clientHeight == 0) && (document.documentElement.clientHeight > 0))
78: {
79: dombody = document.documentElement;
80: }
81: }
82:
83: screenheight = dombody.clientHeight;
84: screenwidth = dombody.clientWidth;
85: pageheight = dombody.scrollHeight;
86: pagewidth = dombody.scrollWidth;
87: centerx = (screenwidth / 2);
88: centery = (screenheight / 2);
89:}
90:addEvent(window, 'load', pageinfo);
91:addEvent(window, 'resize', pageinfo);
92:
93:/**
94: * gets information pertinent to the mouse position on screen and keeps it set
95: * dynamically through the following variables:
96: * screenx x mouse position in relation to the viewable screen area
97: * screeny y mouse position in relation to the viewable screen area
98: * pagex x mouse position in relation to the entire page
99: * pagey x mouse position in relation to the entire page
100: * scrollx x screen scrolled position in relation to the actual page
101: * scrolly y screen scrolled position in relation to the actual page
102: * centerx x position of viewable screen area in relation to the page
103: * centery y position of viewable screen area in relation to the page
104: *
105: * @param event
106: * @return void
107: */
108:var screenx = 0;
109:var screeny = 0;
110:var pagex = 0;
111:var pagey = 0;
112:var scrollx = 0;
113:var scrolly = 0;
114:var centerx = 0;
115:var centery = 0;
116:function mouseinfo(e)
117:{
118: if (!e) var e = window.event;
119:
120: var dombody = document.body;
121:
122: if (document.documentElement)
123: {
124: if ((document.documentElement.scrollTop > 0) || (document.documentElement.scrollLeft > 0))
125: {
126: dombody = document.documentElement;
127: }
128: }
129:
130: var adjustleft = dombody.scrollLeft;
131: var adjusttop = dombody.scrollTop;
132:
133: if (e.pageX || e.pageY)
134: {
135: pagex = e.pageX;
136: pagey = e.pageY;
137: }
138: else if (e.clientX || e.clientY)
139: {
140: pagex = e.clientX + adjustleft;
141: pagey = e.clientY + adjusttop;
142: }
143:
144: scrollx = adjustleft;
145: scrolly = adjusttop;
146: screenx = pagex - scrollx;
147: screeny = pagey - scrolly;
148: centerx = (screenwidth / 2) + scrollx;
149: centery = (screenheight / 2) + scrolly;
150:}
151:addEvent((document.all) ? document : window, 'mousemove', mouseinfo);
152:
153:/**
154: * set the value of a form field
155: *
156: * @param string form name
157: * @param string field name
158: * @param string hex color
159: * @return void
160: */
161:function setField(formname, fieldname, value)
162:{
163: if (document.forms[formname])
164: {
165: if (document.forms[formname].elements[fieldname])
166: {
167: document.forms[formname].elements[fieldname].value = value;
168: }
169: }
170:}
171:
172:/**
173: * set the index of a select field
174: *
175: * @param string form name
176: * @param string field name
177: * @param string hex color
178: * @return void
179: */
180:function setFieldIndex(formname, fieldname, index)
181:{
182: if (document.forms[formname])
183: {
184: if (document.forms[formname].elements[fieldname])
185: {
186: if (document.forms[formname].elements[fieldname].type == 'select-one')
187: {
188: document.forms[formname].elements[fieldname].selectedIndex = index;
189: }
190: }
191: }
192:}
193:
194:/**
195: * submit a form
196: *
197: * @param string form name
198: * @return void
199: */
200:function submitForm(formname)
201:{
202: document.forms[formname].submit();
203:}
204:
205:/**
206: * change a form fields background color
207: *
208: * @param string form name
209: * @param string field name
210: * @param string hex color
211: * @return void
212: */
213:function changeFieldColor(formname, fieldname, color)
214:{
215: if (document.forms[formname])
216: {
217: document.forms[formname].elements[fieldname].style.backgroundColor = color;
218: }
219:}
220:
221:/**
222: * convert hexadecimal to rgb
223: *
224: * @param string hex color
225: * @return array array(r, g, b), null on failure
226: */
227:function hex2rgb(hex)
228:{
229: var regexp = new RegExp('^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$');
230: var match = hex.toLowerCase().match(regexp);
231:
232: if (match)
233: {
234: var rgb = new Array(parseInt(match[1], 16), parseInt(match[2], 16), parseInt(match[3], 16));
235: return(rgb);
236: }
237: else
238: {
239: return(null);
240: }
241:}
242:
243:/**
244: * convert rgb to hexadecimal
245: *
246: * @param integer red
247: * @param integer green
248: * @param integer blue
249: * @return string hex color
250: */
251:function rgb2hex(red, green, blue)
252:{
253: var hex = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
254: var rgb = new Array();
255: var k = 0;
256:
257: for (var i = 0; i < 16; i++) for (var j = 0; j < 16; j++) { rgb[k] = hex[i] + hex[j]; k++ }
258:
259: return('#' + rgb[red] + rgb[green] + rgb[blue]);
260:}
261:
262:/**
263: * human friendly numeric sort
264: *
265: * @param array array to sort
266: * @return array sorted array
267: */
268:function sortarray(arr)
269:{
270: return arr.sort(
271: function(a, b)
272: {
273: return (a.match(/d+/) - b.match(/d+/));
274: }
275: );
276:}
277:
Download Source
permalink
|
Natural Living (5) Heating & Cooling (1) Herbal Remedies (1) Personal (0) Family (1) Humor (11) Miscellaneous (1) Politics (5) Technology (2) System Administration (4) Linux (1) Solaris (0) Web Development (2) CSS (3) Design (1) Flash (1) JavaScript (11) PHP (1) CakePHP (1) Web Browsers (2) Firefox (1) Internet Exploder (0) Netscape (1)
|