Never been to DZone Snippets before?

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

About this user

Benoit Asselin http://www.ab-d.fr

« Newer Snippets
Older Snippets »
Showing 11-17 of 17 total

count() in JavaScript

The same function as count() in php.

   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

in_array() in JavaScript

The same function as in_array() in php.

   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  }


Sample :
   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


Source: PHP Manual in_array()

Include CSS Stylesheet by DOM

   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  


Source: Document Object Model (DOM) Level 1 Specification

IE doesn't support element.setAttritube('style')

   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>

Another way of written "target"

Another way of written "target" and valid in XHTML

   1  
   2  
   3  <a href="http://www.google.com/" onclick="window.open(this.href); return false;">Link</a>
   4  

Save preference key for Widgets ( Dashboard - Mac OSX )

Save preferences
   1  
   2  if (window.widget) {
   3  	widget.setPreferenceForKey(value, 'myKey');
   4  }


Load preferences
   1  
   2  if (window.widget) {
   3  	if (!(widget.preferenceForKey('myKey') === undefined)) {
   4  		var myKey = widget.preferenceForKey('myKey');
   5  	}
   6  }

Fix double keypress with Safari

Fix a bug with the event keypress ( double keypress ... ) with Safari ( Mac OSX Tiger )
   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  }

Sample
   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  ...
« Newer Snippets
Older Snippets »
Showing 11-17 of 17 total