Skip to content

Instantly share code, notes, and snippets.

@Macagare
Macagare / gist:4072116
Created November 14, 2012 13:40
Javascript: Script link with fallback
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="http://www.myurl.com/js/jquery-1.4.2.min.js">\x3C/script>')</script>
@Macagare
Macagare / gist:4058166
Created November 12, 2012 08:32
JavaScript: class (using object literals)
var apple = {
type: "macintosh",
color: "red",
getInfo: function () {
return this.color + ' ' + this.type + ' apple';
}
}
@Macagare
Macagare / gist:4058163
Created November 12, 2012 08:32
Javascript: Class (using a function)
function Apple (type) {
this.type = type;
this.color = "red";
this.getInfo = function() {
return this.color + ' ' + this.type + ' apple';
};
}
// Or, this other way:
@Macagare
Macagare / gist:4046188
Created November 9, 2012 15:02
Javascript: jquery find parent
//Find a parent div by id where display is none:
/*
<div id="#extended_selectors"...>
...
<span id="lorem"... />
</div>
*/
if( $(id).parents().find("#extended_selectors").css('display') == "none" ) {
console.log("found");
}
@Macagare
Macagare / gist:4044474
Created November 9, 2012 08:35
Javascript: Lazy Loading Images
/*
ressource: http://css-tricks.com/snippets/javascript/lazy-loading-images/?utm_source=dlvr.it&utm_medium=twitter
*/
<img src="blank.gif" class="lazy" data-src="/images/full-size.jpg" width="240" height="152"></a>
/* lazyload.js (c) Lorenzo Giuliani
* MIT License (http://www.opensource.org/licenses/mit-license.html)
*
* expects a list of:
@Macagare
Macagare / gist:4044440
Created November 9, 2012 08:28
Javascript: getInternetExplorerVersion()
/**
* Returns the version of Internet Explorer or a -1
* (indicating the use of another browser).
*/
function getInternetExplorerVersion()
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
@Macagare
Macagare / gist:4024294
Created November 6, 2012 12:08
MySQL: Create db and user
$ mysql -uroot -p
mysql> create database my_new_database;
mysql> grant all on my_new_database.* to new_user@localhost identified by 'my secret password';
mysql> flush privileges;
mysql> quit
@Macagare
Macagare / gist:4017991
Created November 5, 2012 16:04
Javascript: jquery handcursor mouseover
// http://jsfiddle.net/jquerybyexample/RgLPC/
$(".ui-datepicker-trigger").mouseover(function() {
$(this).css('cursor', 'pointer');
});
@Macagare
Macagare / gist:4001075
Created November 2, 2012 12:33
Actionscript: Calc if point is within shape
var userClickTarget : Point = new Point(tmpXPos, tmpYPos);
// read bounds extremes
var minAreaX : Number = -1;
var maxAreaX : Number = -1;
var minAreaY : Number = -1;
var maxAreaY : Number = -1;
// when an area specified, convert for flash interface to be able to debug display
var posLen : uint = targetPosition.xList.length;
@Macagare
Macagare / gist:3999514
Created November 2, 2012 08:37
Javascript: restrict textinput characters
<script type="text/javascript">
$(document).ready(function() {
$('.phoneInput').keypress(function(key) {
if(key.charCode < 48 || key.charCode > 57) return false;
});
$('.surnameInput').keypress(function(key) {
if((key.charCode < 97 || key.charCode > 122) && (key.charCode < 65 || key.charCode > 90) && (key.charCode != 45)) return false;
});
</script>