count() in JavaScript
1 2 Array.prototype.count = function() { 3 return this.length; 4 };
Sample :
1 2 var v_array = [ 5, 10, 15, 20, 25]; 3 document.writeln(v_array.count()); // 5
Source: AB-D.fr
DZone Snippets > ki4ngel > javascript
13403 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Benoit Asselin http://www.ab-d.fr
1 2 Array.prototype.count = function() { 3 return this.length; 4 };
1 2 var v_array = [ 5, 10, 15, 20, 25]; 3 document.writeln(v_array.count()); // 5
1 2 Array.prototype.in_array = function(p_val) { 3 for(var i = 0, l = this.length; i < l; i++) { 4 if(this[i] == p_val) { 5 return true; 6 } 7 } 8 return false; 9 }
1 2 var v_array = [ 5, 10, 15, 20, 25]; 3 document.writeln(v_array.in_array(10)); // true 4 document.writeln(v_array.in_array(11)); // false
1 2 3 function includeCSS(p_file) { 4 var v_css = document.createElement('link'); 5 v_css.rel = 'stylesheet' 6 v_css.type = 'text/css'; 7 v_css.href = p_file; 8 document.getElementsByTagName('head')[0].appendChild(v_css); 9 } 10
1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" version="-//W3C//DTD XHTML 1.1//EN" xml:lang="fr"> 4 <head> 5 6 <title>.setAttribute('style','');</title> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 8 9 <script type="text/javascript"> 10 <!-- 11 12 window.onload = function() { 13 if (navigator.appName == 'Microsoft Internet Explorer') { 14 document.getElementById('test').style.cssText = 'background:gray; color:white;'; 15 } else { 16 /* document.getElementById('test').style.cssText = 'background:gray; color:white;'; */ 17 document.getElementById('test').setAttribute('style', 'background:gray; color:white;'); 18 } 19 } 20 21 --> 22 </script> 23 24 </head> 25 26 <body> 27 28 <div id="test">document.getElementById('test').setAttribute('style', 'background:gray; color:white;')</div> 29 30 </body> 31 32 </html>
1 2 3 <a href="http://www.google.com/" onclick="window.open(this.href); return false;">Link</a> 4
1 2 if (window.widget) { 3 widget.setPreferenceForKey(value, 'myKey'); 4 }
1 2 if (window.widget) { 3 if (!(widget.preferenceForKey('myKey') === undefined)) { 4 var myKey = widget.preferenceForKey('myKey'); 5 } 6 }
1 2 var v_fixDblKey = 0; 3 function fixDblKey() { 4 if (v_fixDblKey != 0) { 5 return true; 6 } else { 7 v_fixDblKey = setTimeout('v_fixDblKey = 0;', 10); 8 return false; 9 } 10 }
1 2 ... 3 inputOnkeyup : function(event) { 4 if (fixDblKey()) { return; } 5 switch (event.keyCode) { 6 case 38 : /* up */ 7 break 8 case 40 : /* down */ 9 break; 10 case 37 : /* left */ 11 break; 12 case 39 : /* right */ 13 break; 14 case 9 : /* tab */ 15 break; 16 case 13 : /* enter */ 17 break; 18 } 19 } 20 ...