Skip to content

Instantly share code, notes, and snippets.

View fael's full-sized avatar

Rafael Santos Sá fael

  • OLX
  • Lisbon, Portugal
View GitHub Profile
@fael
fael / gist:1212224
Created September 12, 2011 19:59
Get Query String parameters
//I found it @ util.js / chrome 16.
function parseQueryParams(location) {
var params = {};
var query = unescape(location.search.substring(1));
var vars = query.split("&");
for (var i=0; i < vars.length; i++) {
var pair = vars[i].split("=");
params[pair[0]] = pair[1];
}
@fael
fael / gist:1212309
Created September 12, 2011 20:27
Javascript code organization
//useful snippet to organize website code or widget code.
var MySite = {}
MySite.setupDOM = function(){
//dom manipulation, etc...
}
MySite.setupEvents = function(){
//binds, event handlers, event listeners
@fael
fael / gist:1212350
Created September 12, 2011 20:44
jQuery UI Alert (with alert() override)
getOrCreateDialog = function (id) {
$box = $('#' + id);
if (!$box.length) {
$box = $('<div id="' + id + '"><p></p></div>').hide().appendTo('body');
}
return $box;
}
customAlert(message, title, options, callback){
callback = callback || function(){};
@fael
fael / gist:1212388
Created September 12, 2011 20:55
Validar CNPJ
validarCNPJ = function (cnpj) {
var numeros,
digitos,
soma,
i,
resultado,
pos,
tamanho,
digitos_iguais;
@fael
fael / gist:1212402
Created September 12, 2011 20:59
Identificar Resolução
var identificarResolucao = function() {
resolucao = "res" + screen.width;
areaVisivel = $("body").width();
//$("#componente").prepend("<br>"+ resolucao);
classe = 'res';
if (areaVisivel < 800) classe += '800';
else if (areaVisivel < 960) classe += '960';
else if (areaVisivel < 1024) classe += '1024';
else if (areaVisivel < 1088) classe += '1088';
@fael
fael / gist:1212419
Created September 12, 2011 21:03
Some Array Functions
in_array = function(needle, haystack, argStrict) {
// Checks if the given value exists in the array
var found = false,
key, strict = !! argStrict;
for (key in haystack) {
if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
found = true;
break;
}
@fael
fael / gist:1212422
Created September 12, 2011 21:04
Get and format tweets
getTweets = function(alvo, usuario, qtdMensagens){
var o = {}
o.qtdMensagens = qtdMensagens;
o.caixaTwitter = $("#" + alvo);
o.caixaTwitter.append("<li><a href='#'>carregando tweets...</a></li>");
$.jTwitter(usuario, o.qtdMensagens, function(data){
@fael
fael / gist:1212424
Created September 12, 2011 21:04
URL Encode/Decode
$.extend({
URLEncode: function (c) {
var o = '';
var x = 0;
c = c.toString();
var r = /(^[a-zA-Z0-9_.]*)/;
while (x < c.length) {
var m = r.exec(c.substr(x));
if (m != null && m.length > 1 && m[1] != '') {
o += m[1];
@fael
fael / gist:1212432
Created September 12, 2011 21:07
Force scroll when jQuery Dialog is open
function enableScrollbar(event, ui) {
window.setTimeout(function() {
$(document)
.unbind('mousedown.dialog-overlay')
.unbind('mouseup.dialog-overlay');
}, 100);
}
//when calling a dialog, override the open method
$(...).dialog({
@fael
fael / gist:1212439
Created September 12, 2011 21:09
Prevent event propagation
kill = function (e) {
if (!e) e = window.event;
(e.stopPropagation) ? e.stopPropagation() : e.cancelBubble = true;
(e.preventDefault) ? e.preventDefault() : e.returnValue = false;
return false;
}