Skip to content

Instantly share code, notes, and snippets.

View fczbkk's full-sized avatar
💭
writing Javascript

Riki Fridrich fczbkk

💭
writing Javascript
View GitHub Profile
@fczbkk
fczbkk / coder-test.js
Created November 12, 2012 13:10
Kóderský test
var rootElm = document.getElementById('content');
var paragraphs = rootElm.getElementsByTagName('p');
for (i = 0; i < paragraphs.length; i++) {
rootElm.removeChild(paragraphs[i]);
}
/*
OTÁZKY
- Čo chcel autor týmto kódom dosiahnuť?
@fczbkk
fczbkk / event-bridge.coffee
Last active December 12, 2015 05:28
Simple bridge pattern to unify event listeners between IE and the standard way.
addEvent = (object, event, callback = ->) ->
if object.addEventListener
object.addEventListener event, callback, false
else if object.attachEvent
object.attachEvent "on#{event}", callback
removeEvent = (object, event, callback = ->) ->
if object.removeEventListener
object.removeEventListener event, callback, false
else if object.detachEvent
@fczbkk
fczbkk / ajax.js
Created March 25, 2013 07:06
Minimalist AJAX function for the most basic calls. Only supports GET method. Loads given url and sends result to the callback function. Callback attribute is optional, sometimes you just want to ping the server and you don't care about the response.
function ajax (url, callback) {
callback = callback || function () {};
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
callback(xhr.responseText);
}
};
xhr.open("GET", url, true);
xhr.send();
@fczbkk
fczbkk / get-random-string.js
Created March 25, 2013 12:29
Generates a random string and returns them or sends it to callback. Great for generating unique identifiers.
/*
Has two optional options parameters:
- length - 8 characters by default
- characters - upper- and lowercase characters and numbers by default
*/
function getRandomString(options, callback) {
options = options || {};
options.length = options.length || 8;
options.characters = options.characters || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
@fczbkk
fczbkk / merge-url-params.coffee
Created November 14, 2013 10:23
Merges provided list of parameters and their values into a string usable in URL.
mergeUrlParams = (params = {}) -> ("#{key}=#{val}" for key, val of params).join '&'
unless Array::indexOf
Array::indexOf = (search_element, from_index) ->
throw new TypeError '"this" is null or not defined' unless this?
length = @length >>> 0 # Hack to convert object.length to a UInt32
from_index = +from_index or 0
from_index = 0 if Math.abs(from_index) is Infinity
if from_index < 0
from_index += length
@fczbkk
fczbkk / emulate-click-event.coffee
Last active August 29, 2015 14:03
Emulates click event, very useful when testing UI features.
simulateClick = (elm) ->
evt = document.createEvent 'MouseEvents'
evt.initMouseEvent(
# type, canBubble, cancelable, view,
'click', true, true, window
# detail, screenX, screenY, clientX, clientY,
0, 0, 0, 0, 0
# ctrlKey, altKey, shiftKey, metaKey,
false, false, false, false
# button, relatedTarget
ajax = (url, callback = ->) ->
xhr = new XMLHttpRequest()
xhr.onreadystatechange = ->
if xhr.readyState is 4
if xhr.status in [200, 304]
callback xhr.responseText
xhr.open 'GET', url, true
xhr.send()
@fczbkk
fczbkk / class-names.coffee
Created July 25, 2014 06:57
Bridge for working with element classes (has, add, remove). It would be much easier to use `element.classList` (https://developer.mozilla.org/en-US/docs/Web/API/Element.classList), but unfortunately, Internet Explorer forgot to implement it until version 10.
getClassNameRe = (classname) ->
///
(^|\s) # beginning of string or whitespace
#{classname}
(\s|$) # end of string or whitespace
///
hasClassName = (element, classname) ->
getClassNameRe(classname).test element.className
@fczbkk
fczbkk / is_array.coffee
Created August 24, 2014 05:07
Cross browser bridge to check, if provided object is an array.
isArray = Array.isArray or (obj) -> Object::toString.call obj is '[object Array]'