you are here: codestackercodes [RSS]

good practice to css typografic

Via http://goo.gl/O4yzj

h1, h2, h3, h4, h5, h6 { line-height: 1.1; }
body { font: 16px/1.5 "Lucida Grande", Verdana, sans-serif }

get database's size

select table_schema "Database Name", round(sum(data_length + index_length) / 1024 / 1024, 1) "Database Size (MB)" 
from information_schema.TABLES group by table_schema;

restart springboard

killall -HUP SpringBoard

getElementByClassName

function getElementsByClassName(node,classname) {
  if (node.getElementsByClassName) { // use native implementation if available

    return node.getElementsByClassName(classname);
  } else {
    return (function getElementsByClass(searchClass,node) {
        if ( node == null )
          node = document;
        var classElements = [],
            els = node.getElementsByTagName("*"),
            elsLen = els.length,
            pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;

        for (i = 0, j = 0; i < elsLen; i++) {
          if ( pattern.test(els[i].className) ) {
              classElements[j] = els[i];
              j++;
          }
        }
        return classElements;
    })(classname, node);
  }
}

var elements = getElementsByClassName(document, "myclass"); // example

detecting popup blockers

var win = window.open(...);
if(win == null || typeof(win) == "undefined" || (win == null && win.outerWidth == 0) || (win != null && win.outerHeight == 0) || win.test == "undefined") {
// popup blocked :(

}

simple date validation

function dateValidation() {

	var obj = document.getElementById("my_field"); // my_field: 12/12/2011

	var day = obj.value.split("/")[0];

	var month = obj.value.split("/")[1];
	var year = obj.value.split("/")[2];

	if ((day<1 || day >31) || (month<1&&month>12)&&(year.length != 4)) {
		alert("Invalid format");
		return false;
	} else {

		var dt = new Date(year, month-1, day);
		var today = new Date();

		if((dt.getDate() != day) || (dt.getMonth() != month-1) || (dt.getFullYear()!=year) || (dt>today)) {
			alert("Invalid date");
			return false;
		}
	}
}

dumping mysql store procedures, functions and triggers

mysqldump -u user -p -h localhost your_database --routines --no-create-info --no-data --no-create-db --skip-opt > routines.sql

list all apache loaded modules

apache2ctl -t -D DUMP_MODULES

convert mysql dump to sqlite database

via http://goo.gl/p7Sxj

#!/bin/bash


if [ "x$1" == "x" ]; then
  echo "First: mysqldump -u root -p --compatible=ansi --skip-opt database > dumpfile"
  echo "Usage: $0 <dumpfile>"
  exit
fi

cat $1 |
grep -v ' KEY "' |
grep -v ' UNIQUE KEY "' |
grep -v ' PRIMARY KEY ' |
sed '/^SET/d' |
sed 's/ unsigned / /g' |
sed 's/ auto_increment/ primary key autoincrement/g' |
sed 's/ smallint([0-9]*) / integer /g' |
sed 's/ tinyint([0-9]*) / integer /g' |
sed 's/ int([0-9]*) / integer /g' |
sed 's/ character set [^ ]* / /g' |
sed 's/ enum([^)]*) / varchar(255) /g' |
sed 's/ on update [^,]*//g' |
sed 's/\\r\\n/\\n/g' |
sed 's/\\"/"/g' |
perl -e 'local $/;$_=<>;s/,\n\)/\n\)/gs;print "begin;\n";print;print "commit;\n"' |
perl -pe '
if (/^(INSERT.+?)\(/) {
  $a=$1;
  s/\\'\''/'\'\''/g;
  s/\\n/\n/g;
  s/\),\(/\);\n$a\(/g;
}
' > $1.sql
cat $1.sql | sqlite3 $1.db > $1.err
ERRORS=`cat $1.err | wc -l`
if [ $ERRORS == 0 ]; then
  echo "Conversion completed without error. Output file: $1.db"
  rm $1.sql
  rm $1.err
else
  echo "There were errors during conversion.  Please review $1.err and $1.sql for details."
fi

how to use number_to_currency in controller

include ActionView::Helpers::NumberHelper

background animation

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

$.fn.backgroundAnimation : function(bgcolor, fgcolor, duration) {
	$(this).animate({ backgroundColor: bgcolor }, duration).css('color', fgcolor);
}

highlight an element

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

$.fn.highlight : function (highlightColor, duration) {
    var highlightBg = highlightColor || "#FFFF9C";
    var animateMs = duration || 2000;
    var originalBg = this.css("background-color");

    if (!originalBg || originalBg == highlightBg)
        originalBg = "#FFFFFF"; // default to white


    $(this).css("backgroundColor", highlightBg).animate({ backgroundColor: originalBg }, animateMs, null, function () { $(this).css("backgroundColor", originalBg);  });
}

center vertically element

$.fn.center = function (relation) {
    var center_in = (relation) ? $(relation) : $(window);
    
    this.css("position","absolute");
    this.css("top", ( center_in.height() - this.height() ) / 2+center_in.scrollTop() + "px");
    this.css("left", ( center_in.width() - this.width() ) / 2+center_in.scrollLeft() + "px");
    return this;
};

simple random string method

def rand_str(length)
	return rand(36**length).to_s(36)
end

reserved javascript words

break
case
catch
continue
default
delete
do
else
finally
for
function
if
in
instanceof
new
return
switch
this
throw
try
typeof
var
void
while
with
abstract
boolean
byte
char
class
const
debugger
double
enum
export
extends
final
float
goto
implements
import
int
interface
long
native
package
private
protected
public
short
static
super
synchronized
throws
transient
volatile

get you public ip

curl ifconfig.me

add to bookmarks using jquery

$("a#bookmark").click(function(){
   var bookmarkUrl = this.href;
   var bookmarkTitle = this.title;
	 
   if ($.browser.mozilla) // For Mozilla Firefox Bookmark

   { 
	window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
   } 
   else if($.browser.msie || $.browser.webkit) // For IE Favorite

   { 
	window.external.AddFavorite( bookmarkUrl, bookmarkTitle); 
   }
   else if($.browser.opera ) // For Opera Browsers

   { 
	$(this).attr("href",bookmarkUrl);
	$(this).attr("title",bookmarkTitle);
	$(this).attr("rel","sidebar");
	$(this).click();
   } 
   else // for other browsers which does not support

   { 
        alert('Please hold CTRL+D and click the link to bookmark it in your browser.');
   }
   return false; 
});

sanitize function

<?
function sanitize($data)
{
	$data = trim($data); 
	if(get_magic_quotes_gpc())
	{
		$data = stripslashes($data);
	}
	$data = mysql_real_escape_string($data);
	return $data;
}

//example
$username = sanitize($_POST['username']);
?>

transition to link highlighting

a {
        transition: all .2s linear; 
        -o-transition: all .2s linear;
        -moz-transition: all .2s linear;
        -webkit-transition: all .2s linear;
}

useful html5 attributes for iOS

<!-- For a username, you don't want to autocorrect, nor do you want a capitalised first letter. -->
<input type="text" autocorrect="off" autocapitalize="off">

<!-- Turn telephone detection off -->
<meta name="format-detection" content="telephone=no" />

<!-- Make webpage an app (get rid of top bar) when bookmarked to home screen -->
<meta name="apple-mobile-web-app-capable" content="yes" />

<!-- Number keyboards -->
<input type="tel" /> <input pattern="[0-9]*">

<!-- URL/Emails -->
<input type="url"> <input type="email">

<!-- Links -->
<a href="http://maps.google.com/maps?q=Moon">Open in Google Maps</a>
<a href="tel:01234567890">Open the phone</a>
<a href="twitter:Tweet Tweet Tweet">Open the Twitter for iPhone app</a>
Displaying Code 21 - 40 of 301 in total