you are in: codestackercodes [RSS] → tag: javascript [RSS]

getElementByClassName Delicious Email

show/hide lines
   1  function getElementsByClassName(node,classname) {
   2    if (node.getElementsByClassName) { // use native implementation if available
   3      return node.getElementsByClassName(classname);
   4    } else {
   5      return (function getElementsByClass(searchClass,node) {
   6          if ( node == null )
   7            node = document;
   8          var classElements = [],
   9              els = node.getElementsByTagName("*"),
  10              elsLen = els.length,
  11              pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;
  12  
  13          for (i = 0, j = 0; i < elsLen; i++) {
  14            if ( pattern.test(els[i].className) ) {
  15                classElements[j] = els[i];
  16                j++;
  17            }
  18          }
  19          return classElements;
  20      })(classname, node);
  21    }
  22  }
  23  
  24  var elements = getElementsByClassName(document, "myclass"); // example
created by leozera — 21 November 2011 — get a short url — tags: javascript embed

detecting popup blockers Delicious Email

show/hide lines
   1  var win = window.open(...);
   2  if(win == null || typeof(win) == "undefined" || (win == null && win.outerWidth == 0) || (win != null && win.outerHeight == 0) || win.test == "undefined") {
   3  // popup blocked :(
   4  }
created by leozera — 17 November 2011 — get a short url — tags: javascript popup embed

simple date validation Delicious Email

show/hide lines
   1  function dateValidation() {
   2  
   3  	var obj = document.getElementById("my_field"); // my_field: 12/12/2011
   4  	var day = obj.value.split("/")[0];
   5  
   6  	var month = obj.value.split("/")[1];
   7  	var year = obj.value.split("/")[2];
   8  
   9  	if ((day<1 || day >31) || (month<1&&month>12)&&(year.length != 4)) {
  10  		alert("Invalid format");
  11  		return false;
  12  	} else {
  13  
  14  		var dt = new Date(year, month-1, day);
  15  		var today = new Date();
  16  
  17  		if((dt.getDate() != day) || (dt.getMonth() != month-1) || (dt.getFullYear()!=year) || (dt>today)) {
  18  			alert("Invalid date");
  19  			return false;
  20  		}
  21  	}
  22  }
created by leozera — 16 November 2011 — get a short url — tags: date javascript embed

background animation Delicious Email

requires jquery.ui or jquery.color (https://github.com/jquery/jquery-color)

show/hide lines
   1  $.fn.backgroundAnimation : function(bgcolor, fgcolor, duration) {
   2  	$(this).animate({ backgroundColor: bgcolor }, duration).css('color', fgcolor);
   3  }
created by leozera — 31 July 2011 — get a short url — tags: jquery embed

highlight an element Delicious Email

requires jquery.ui or jquery.color (https://github.com/jquery/jquery-color)

show/hide lines
   1  $.fn.highlight : function (highlightColor, duration) {
   2      var highlightBg = highlightColor || "#FFFF9C";
   3      var animateMs = duration || 2000;
   4      var originalBg = this.css("background-color");
   5  
   6      if (!originalBg || originalBg == highlightBg)
   7          originalBg = "#FFFFFF"; // default to white
   8  
   9      $(this).css("backgroundColor", highlightBg).animate({ backgroundColor: originalBg }, animateMs, null, function () { $(this).css("backgroundColor", originalBg);  });
  10  }
created by leozera — 31 July 2011 — get a short url — tags: jquery embed

center vertically element Delicious Email

show/hide lines
   1  $.fn.center = function (relation) {
   2      var center_in = (relation) ? $(relation) : $(window);
   3      
   4      this.css("position","absolute");
   5      this.css("top", ( center_in.height() - this.height() ) / 2+center_in.scrollTop() + "px");
   6      this.css("left", ( center_in.width() - this.width() ) / 2+center_in.scrollLeft() + "px");
   7      return this;
   8  };
created by leozera — 31 July 2011 — get a short url — tags: jquery embed

reserved javascript words Delicious Email

show/hide lines
   1  break
   2  case
   3  catch
   4  continue
   5  default
   6  delete
   7  do
   8  else
   9  finally
  10  for
  11  function
  12  if
  13  in
  14  instanceof
  15  new
  16  return
  17  switch
  18  this
  19  throw
  20  try
  21  typeof
  22  var
  23  void
  24  while
  25  with
  26  abstract
  27  boolean
  28  byte
  29  char
  30  class
  31  const
  32  debugger
  33  double
  34  enum
  35  export
  36  extends
  37  final
  38  float
  39  goto
  40  implements
  41  import
  42  int
  43  interface
  44  long
  45  native
  46  package
  47  private
  48  protected
  49  public
  50  short
  51  static
  52  super
  53  synchronized
  54  throws
  55  transient
  56  volatile
created by leozera — 19 July 2011 — get a short url — tags: javascript embed

add to bookmarks using jquery Delicious Email

show/hide lines
   1  $("a#bookmark").click(function(){
   2     var bookmarkUrl = this.href;
   3     var bookmarkTitle = this.title;
   4  	 
   5     if ($.browser.mozilla) // For Mozilla Firefox Bookmark
   6     { 
   7  	window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
   8     } 
   9     else if($.browser.msie || $.browser.webkit) // For IE Favorite
  10     { 
  11  	window.external.AddFavorite( bookmarkUrl, bookmarkTitle); 
  12     }
  13     else if($.browser.opera ) // For Opera Browsers
  14     { 
  15  	$(this).attr("href",bookmarkUrl);
  16  	$(this).attr("title",bookmarkTitle);
  17  	$(this).attr("rel","sidebar");
  18  	$(this).click();
  19     } 
  20     else // for other browsers which does not support
  21     { 
  22          alert('Please hold CTRL+D and click the link to bookmark it in your browser.');
  23     }
  24     return false; 
  25  });
created by leozera — 17 July 2011 — get a short url — tags: jquery embed

detecting ie version with jquery Delicious Email

show/hide lines
   1  var IE = $.browser.msie, IEv = $.browser.version;
   2  
   3  if (IE) { //could be improved
   4      var IE6 = parseInt(IEv) == 6,
   5          IE7 = parseInt(IEv) == 7, 
   6          IE8 = parseInt(IEv) == 8, 
   7          IE9 = parseInt(IEv) == 9;
   8  }
   9  
  10  //Usage:
  11  
  12  if (IE) {
  13      //Do something for all IE versions
  14  }
  15  if (IE7) {
  16      //Do stuff just for IE7
  17  }
  18  if (IE7 || IE8) {
  19      //Do stuff just for IE7 or IE8
  20  }
  21  if (!IE6) {
  22      //Do something if not IE6
  23  }
created by leozera — 11 July 2011 — get a short url — tags: browser ie jquery embed

simple jquery table sort Delicious Email

requires jquery.sortElements.js (https://github.com/jamespadolsey/jQuery-Plugins/blob/master/sortElements/jquery.sortElements.js)

via http://stackoverflow.com/questions/3160277/jquery-table-sort

show/hide lines
   1  var table = $('table');
   2  
   3  $('th').wrapInner('<span title="Sort this column"/>').each(function(){
   4       var th = $(this), thIndex = th.index(), inverse = false;
   5       th.click(function(){
   6           table.find('td').filter(function(){
   7               return $(this).index() === thIndex;
   8           }).sortElements(function(a, b){
   9               return $.text([a]) > $.text([b]) ?
  10                   inverse ? -1 : 1
  11                   : inverse ? 1 : -1;
  12           }, function(){
  13  			return this.parentNode; 
  14           });
  15           inverse = !inverse;
  16       });
  17  });
created by leozera — 01 July 2011 — get a short url — tags: jquery table embed
Displaying records 1 - 10 of 49