mrkeck Sharing the Love

15Dec/120

Comment Out a Portion of Code Convention

if(0){  //Change 0 to 1 to turn on code segment
     //Code that I want to turn on and off
}
1Dec/120

jQuery Full Position of Element on Page

var eleBounds=function(ele){
	ele=$(ele);
	var off=ele.offset();
	var bounds={};
	bounds.top=off.top;
	bounds.bottom=bounds.top+ele.height();
	bounds.left=off.left;
	bounds.right=bounds.left+ele.width();
	bounds.height=ele.height();
	bounds.width=ele.width();
	return bounds;
}

Works well with phantomJS clipRect, just have to get the function in the page and return back the object and set clipRect equal to it.

Filed under: jquery, phantomJs No Comments
26Nov/120

Javascript: Faster Math.floor and parseInt

var str='123.55';
var num1=~~str; //num=123
var num2=Math.floor(str); //num=123
var num3=parseInt(str); //num=123

~~ operator gives the same result as Math.floor and parseInt but is slightly faster than Math.floor and is 6 times faster than parseInt.
Benchmark http://jsperf.com/rounding-numbers-down/15

Filed under: Optimization No Comments
26Nov/120

Javascript sleep function

var sleepyTime=10*1000;  //10 seconds
setTimeout(function(){
//In 10 seconds this will alert("I'm awake!");
alert("I'm awake!");
},sleepyTime);
12Nov/120

phantomJs delay until condition is met

var tries=0;
var maxTries=10;
var howOften=2000;
var tom=setInterval(function(){
	tries++;
	delayResponse=page.evaluate(function(){
		//console.log('Searching');
		return $('#thingy').length>0;
	});
	if(delayResponse || maxTries<=tries){
		//console.log('Found it or out of time');
		clearInterval(tom);
		//Do stuff now that it is ready
	}
},howOften);

There is a function out there called waitFor but I found this easier to handle

4Aug/120

Simulate Mouse Click with Javascript/jQuery

//Find thing you want to click
var ele=$('.thingToClick')[0];
 
//Function that performs the click
var doClick=function(ele){
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
	0, 0, 0, 0, 0, false, false, false, false, 0, null);
  return ele.dispatchEvent(evt);
};
 
//Call the click
doClick(ele);
Filed under: Javascript, jquery No Comments
23Jan/120

jQuery Animation Queue

$('#logo').stop().animate({'left':(curLeft)},500);

http://www.learningjquery.com/2009/01/quick-tip-prevent-animation-queue-buildup

Filed under: jquery No Comments
18Nov/110

jQuery Mobile breaks autoFill on iPhone

To turn autoFill back on use:
$('input[type=text]').attr('autocomplete','on');

For some reason they set it to off by default.

Filed under: jQueryMobile, web No Comments
16Nov/110

Javascript Perfomance

http://jsperf.com/

Lets you try out different variations and tests them on several browsers to show you which way is faster.

Pretty Awesome!

29Mar/110

jQuery Graphing

Highcharts looks great!  If I were doing a non-commercial site I would check this one out heavily.  But since it costs, I'm on to the next one.

jqPlot worked pretty well but had some issues for me.  Some of these issues are most likely me just not knowing what I am doing but I like a library to handle my ignorance.  The data points didn't line up with the grid and when I used a bar graph I couldn't get the x-axis labels to format into a two decimal number.  It looked like it had options to do it but they didn't work for me.

Flot has worked great for me so far.  I like the tooltip, hover, and click stuff.  Looks nice too.  I had the initial call wrong and that made nothing work but I have it right now and am very pleased.