Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Petar Bojinov pbojinov

🏠
Working from home
View GitHub Profile
@pbojinov
pbojinov / bookmarklet-jquery.js
Last active Dec 11, 2015
Bookmarklet - Add jquery to page
View bookmarklet-jquery.js
javascript:(function(){
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js';
var head = document.getElementsByTagName('head')[0];
head.appendChild(script);
}());
View msconvert.js
/**
* Convert milliseconds to Hour:Minute:Seconds || Minute:Seconds
*
* @param {int} milliseconds
* @return {Object} h = hour, m = minute, s = seconds
*/
function convertMS(ms) {
var d, h, m, s;
s = Math.floor(ms / 1000);
m = Math.floor(s / 60);
@pbojinov
pbojinov / jsonp.js
Last active Dec 12, 2015
Add jQuery to page in JSONP like manner
View jsonp.js
/**
* Add jQuery to page in JSONP like manner
*/
(function() {
var head = document.getElementsByTagName('head')[0],
script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js';
head.appendChild(script);
})();
View String.prototype.shuffle.js
/**
* Shuffle string like crazy
*
* @param {int} length desired
* @return {string} the shuffled string
*/
String.prototype.shuffle = function(size) {
var arr = this.split('');
for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x){};
arr = arr.join('');
@pbojinov
pbojinov / uri.js
Last active Apr 8, 2018 — forked from jlong/uri.js
Easy URI parsing by creating an anchor element and accessing its properties
View uri.js
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
@pbojinov
pbojinov / String.prototype.contains.js
Last active Dec 13, 2015
String.prototype.contains
View String.prototype.contains.js
/**
* Wrapper for indexOf() to return true || false if string is contained within another string
* Will not not extend String if 'contains' already exists
*
* @param {String}
* @return {Boolean}
*/
if (typeof String.prototype.contains === 'undefined') {
String.prototype.contains = function(it) {
return this.indexOf(it) !== -1;
@pbojinov
pbojinov / selector.js
Created Feb 20, 2013
jQuery like selector
View selector.js
var $ = function (selector) {
var cl, id, alls, arrEle = [];
if (/^\./.test(selector)) {
cl = selector.replace(".", "");
alls = document.getElementsByTagName("*"), l = alls.length;
for (var i=0; i<l; i+=1) {
var name = alls[i].className, arrName = [];
if (name) {
arrName = name.split(" "), lName = arrName.length;
for (var j=0; j<lName; j+=1) {
View autoresize.js
jQuery.fn.autoresize = function (options) {
options = options || {}
return this.each(function () {
// Init Sizes
var parent = $(this).parent();
var imageWidth = $(this).width();
var imageHeight = $(this).height();
var parentWidth = parent.width();
var parentHeight = parent.height();
var ratio, newSide;
View tinyxhr.js
// tinyxhr by Shimon Doodkin - licanse: public doamin - https://gist.github.com/4706967
//
// tinyxhr("http://site.com/ajaxaction",function (err,data,xhr){ if (err) console.log("goterr ",err,'status='+xhr.status); console.log(data) });
// tinyxhr("http://site.com/ajaxaction",function (err,data,xhr){ if (err) console.log("goterr ",err,'status='+xhr.status); console.log(data) },'POST','value1=1&value2=2');
// tinyxhr("http://site.com/ajaxaction.json",function (err,data,xhr){ if (err) console.log("goterr ",err,'status='+xhr.status); console.log(data); console.log(JSON.parse(data)) },'POST',JSON.stringify({value:1}),'application/javascript');
// cb - a callback function like: function (err,data,XMLHttpRequestObject){ if (err) throw err; }
//
function tinyxhr(url,cb,method,post,contenttype)
{
@pbojinov
pbojinov / getParentUrl.js
Created Apr 23, 2013
get the URL of an iframe’s parent page, via Nicholas Zaka's article - http://www.nczonline.net/blog/2013/04/16/getting-the-url-of-an-iframes-parent/
View getParentUrl.js
function getParentUrl() {
var isInIframe = (parent !== window),
parentUrl = null;
if (isInIframe) {
parentUrl = document.referrer;
}
return parentUrl;