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

[prototype] adding input:focus functionality to ie Delicious

show/hide lines
   1  Event.observe(window, 'load', function() { 
   2  var fields = $$("input"); 
   3  for (var i = 0; i fields[i].onfocus = function() {this.className += ' focused';} 
   4  fields[i].onblur = function() {this.className = this.className.replace('focused', '');} 
   5  } 
   6  }); 
   7  
   8  // in css, paste
   9  // input:focus, /* works in FF without javascript */ 
  10  // input.focused /* used by js */ 
  11  // { background-color: #f7cd72; } 
created by leozera — 18 November 2008 — get a short url — tags: hack ie6 javascript prototype embed

validate file extension Delicious

a very simple function.

show/hide lines
   1  extensions = new Array(".jpg", ".png");
   2  function limitImage(file, extensions) {
   3  	allowed = false;
   4  	if (!file) return;
   5  
   6  	while (file.indexOf("\\") != -1)
   7  	file = file.slice(file.indexOf("\\") + 1);
   8  	ext = file.slice(file.indexOf(".")).toLowerCase();
   9  	for (var i = 0; i < extensions.length; i++) {
  10  		if (extensions[i] == ext) { allowed = true; break; }
  11  	}
  12  	return allowed;
  13  }
created by leozera — 13 November 2008 — get a short url — tags: javascript validate embed

transparent jbutton Delicious

show/hide lines
   1  Insets noInsets = new Insets(0,0,0,0);
   2  ImageIcon bg = new ImageIcon("bt1.png");
   3  JButton btn = new JButton(bg);
   4  btn.setBounds(0,0,32,32);
   5  btn.setMargin(noInsets);			
   6  btn.setBorder(BorderFactory.createEmptyBorder());
   7  btn.setContentAreaFilled(false);
created by leozera — 03 October 2008 — get a short url — tags: java jbutton transparent embed

show flickr photos with javascript Delicious

show/hide lines
   1  <script type="text/javascript">
   2  function jsonFlickrFeed(o){
   3  	var i = 0;
   4  	while(o.items[i]){
   5      	document.write('<img src="' + o.items[i].media.m + '" alt="' + o.items[i].title +'">');
   6     		i++;
   7  	}
   8  }
   9  </script>
  10  <script src="http://api.flickr.com/services/feeds/photos_public.gne?id=28203925@N08&lang=en-us&format=json" type="text/javascript"></script>
created by leozera — 07 September 2008 — get a short url — tags: flickr javascript json embed

jquery quick start Delicious

source: http://www.shafqatahmed.com/2008/08/mybutton-font-f.html

show/hide lines
   1  // Makes each odd tr element to have a blue backcolor
   2  $("tr:odd").css("background-color", "blue");
   3  
   4  
   5  // What if we want to make the header row have a different look ? Show Me
   6  $("#results tr:first").css("background", "black");
   7  $("#results tr:first").css("color", "white");
   8  $("#results tr:first").css("font-weight", "bold");
   9  
  10  
  11  // We also could have assigned a class name to the header tr element like this 
  12  $("#results tr:first").addClass("header");
  13  
  14  
  15  //All span that are inside a div element (may not be immediate child of the div) will be selected.
  16  $("div span").css("border", "1px solid");
  17  
  18  //Here is how we select the links that are descendent of span which are direct children of a div
  19  $("div > span a")
  20  
  21  //Here is how we can select a div with id mydiv 
  22  $("div#mydiv")
  23  
  24  
  25  // Here is how we select all links under div elements that have css class set to mycss
  26  $("div.mycss a")
  27  
  28  // if we want to find a link that points to http://www.google.com then we would write
  29  $("a[href=http://www.google.com]")
  30  
  31  
  32  // Modifications
  33  $("body").prepend("<img src='banner.jpg' />");
  34  $("div#mydialog").html("<p>Hi There!</p>");
  35  $("#mytable").wrap("<div></div>");
  36  
  37  
  38  // How to replace all hr elements (horizontal line) with a br element
  39  $("<br/>").replaceAll("hr");
created by leozera — 01 September 2008 — get a short url — tags: jquery embed

passing extra parameters to prototype observer handlers Delicious

a simple listener: $(’myForm’).observe(‘submit’, validateMyForm) dont support extra params in the function (in the case, validateMyForm. the solution:

show/hide lines
   1  $('myForm').observe('submit', function(event) {  
   2      validateMyForm(event, param1, param2);  
   3  }); 
created by leozera — 05 August 2008 — get a short url — tags: javascript prototype embed

1kb ie 6 png fix hack Delicious

create a 1×1 ‘clear.gif’ image

show/hide lines
   1  var clear="images/clear.gif" //path to clear.gif
   2  
   3  pngfix=function(){var els=document.getElementsByTagName('*');var i_p=/\.png/i;var i=els.length;while (i-- >0){var el=els[i];var es=el.style;if(el.src&&el.src.match(i_p)&&es.filter==''){el.height = el.height;el.width = el.width;es.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')";el.src = clear;}else{var elb=el.currentStyle.backgroundImage;if(elb.match(i_p)){var path=elb.split('"');var rep=(el.currentStyle.backgroundRepeat=='no-repeat')?'crop':'scale';es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path[1]+"',sizingMethod='"+rep+"')";es.height=el.clientHeight+'px';es.backgroundImage="none";}}if (el.currentStyle.position!='absolute' && !es.filter && !el.tagName.match(/(body|html|script)/gi)) es.position="relative";if (es.filter&&el.currentStyle.position=="relative") es.position="static";}}
   4  window.attachEvent('onload',pngfix);
created by leozera — 17 July 2008 — get a short url — tags: browser css hack ie6 js embed

[funny] google pagerank code Delicious

show/hide lines
   1  function getPagerank(url) {
   2  	// start off with a random low PR
   3  	pagerank = randomNumber(0, 3);
   4  
   5  	if ( pageHostedOn(url, 'google.com') ) {
   6  		pagerank++;
   7  	}
   8  	else if ( pageHostedOn(url, 'microsoft.com') ) {
   9  		pagerank–;
  10  	}
  11  	
  12  	if ( pageValidates(url) ) {
  13  		pagerank *= .5;
  14  	}
  15  	
  16  	tag_value['b'] = 1;
  17  	tag_value['h2'] = 2;
  18  	tag_value['h1'] = 3;
  19  	tag_value['strong'] = -1; // W3C sux!
  20  	pagerank = calculateTagsPr(tag_value, pagerank);
  21  	
  22  	// Sergey said good news sites have
  23  	// lots of nested tables
  24  	tablesOnPage = getTagCount('table');
  25  	if (tablesOnPage >= 50) {
  26  		pagerank += 2;
  27  	}
  28  	if (pagerank >= 5) {
  29  		pagerank = 4; // helps selling AdWords
  30  	}
  31  	if ( linksFrom('mattcutts.com', url) >= 4 ) {
  32  		// I link to “clean” sites only
  33  		// – Matt, Feb 2006
  34  		pagerank += 2;
  35  	}
  36  	
  37  	pagerank += countBacklinks(url) / 10000;
  38  	blacklist1 = getList('government.cn/censored.txt');
  39  	blacklist2 = getList('c:\larry-page-hatelist.txt');
  40  	
  41  	if ( inArray(blacklist1, url) || inArray(blacklist2, url) ) {
  42  		pagerank = 0;
  43  	}
  44  	
  45  	d = dashesInUrl(url);
  46  	pagerank = (d >= 3) ? pagerank -1 : pagerank + 1;
  47  
  48  	if ( inString(url, “how to build a bomb”) ) {
  49  		// added on request. 2004-12-01.
  50  		recipient = “peter@homelandsecurity.gov”;
  51  		subject = “You might wanna check this…”;
  52  		sendMailTo(recipient, subject, url);
  53  		// page might still be relevant
  54  		pagerank++;
  55  	}
  56  
  57  	if ( month() == "June" || month() == "October" ) {
  58  		// makes people talk about
  59  		// PR updates, good publicity
  60  		pagerank -= randomNumber(1,3);
  61  	} 
  62  
  63  	if ( linkCol(url) == WHITE &&
  64  		pageCol(url) == WHITE ) {
  65  		// spammer!! Googleaxe it!!
  66  		pagerank = 0;
  67  	}
  68  
  69  	if (url == “http://www.nytimes.com”) {
  70  		// just testing, pls remove tomorrow
  71  		// – Frank, June 2003
  72  		pagerank = 10;
  73  	}
  74  	return pagerank;
  75  }
created by leozera — 11 July 2008 — get a short url — tags: google humor pagerank embed

resize window bookmarklet Delicious

create a link with this:

show/hide lines
   1  javascript: resizeTo(800,600);
   2  
   3  javascript: resizeTo(1024,768);
created by anonymous — 05 July 2008 — get a short url — tags: bookmarklet javascript embed

"hello world" Delicious

a “hello world” program in javascript

show/hide lines
   1  alert("hello world")
created by anonymous — 02 July 2008 — get a short url — tags: helloworld embed