Skip to content

Instantly share code, notes, and snippets.

View mateuszkocz's full-sized avatar
✌️

Mateusz Kocz mateuszkocz

✌️
View GitHub Profile
//if they DID upload a file...
if($_FILES['photo']['name'])
{
//if no errors...
if(!$_FILES['photo']['error'])
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
{
@mateuszkocz
mateuszkocz / insertAdjacentHTML
Created August 6, 2013 16:34
insertAdjacentHTML(). Performant way of inserting HTML into a DOM. Source: http://ejohn.org/blog/dom-insertadjacenthtml/
var ul = document.getElementById("list");
ul.insertAdjacentHTML("beforeEnd", "<li>A new li on the list.</li>");
ul.insertAdjacentHTML("beforeEnd", "<li>Another li!</li>");
.insertAdjacentHTML("beforeBegin", ...)
.insertAdjacentHTML("afterBegin", ...)
.insertAdjacentHTML("beforeEnd", ...)
.insertAdjacentHTML("afterEnd", ...)
@mateuszkocz
mateuszkocz / classes.js
Created July 20, 2013 16:04
Dealing with classes (add, remove, has). Source: http://blog.ponyfoo.com/2013/07/09/getting-over-jquery
!function(exports){
var class_list = !!document.body.classList;
var s = '(\\s|^)'; // space or start
var e = '(\\s|$)'; // space or end
function getRegex(className){
return new RegExp(s + className + e, 'g');
}
exports.addClass = function(element, className){
<form id='registration' name='registration' action='/register'>
<input type='text' name='username' value='carlos'>
<input type='email' name='email' value='[email protected]'>
<input type='number' name='dob' value='1940'>
<input type='submit' onclick='return sendForm(this.form);'>
</form>
@mateuszkocz
mateuszkocz / extend-object
Created July 20, 2013 12:49
Extends object.
function extend( a, b ) {
for( var key in b ) {
if( b.hasOwnProperty( key ) ) {
a[key] = b[key];
}
};
return a;
}
@mateuszkocz
mateuszkocz / css
Created July 19, 2013 19:04
Disable image smoothing. Comes in handy when creating pixel-art game. The pixels will stay sharp.
/* You can aslo add */
canvas {
image-rendering: optimizeSpeed;
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-optimize-contrast;
image-rendering: optimize-contrast;
-ms-interpolation-mode: nearest-neighbor;
}
function getOffset( el ) {
var offsetTop = 0, offsetLeft = 0;
do {
if ( !isNaN( el.offsetTop ) ) {
offsetTop += el.offsetTop;
}
if ( !isNaN( el.offsetLeft ) ) {
offsetLeft += el.offsetLeft;
}
} while( el = el.offsetParent )
// Instead of this..
if(callback) {
callback();
}
// you can use this.
callback && callback();
Element.prototype.on = Element.prototype.addEventListener;
@mateuszkocz
mateuszkocz / jquery-like-selector
Created June 24, 2013 16:10
jQuery/Sizzle like dollar sign element selector. Source: http://remysharp.com/2013/04/19/i-know-jquery-now-what
var $ = document.querySelectorAll.bind(document);