Skip to content

Instantly share code, notes, and snippets.

@unknownuser88
unknownuser88 / gist:8284854
Created January 6, 2014 15:55
jQuery create element
jQuery('<div/>', {
id: 'foo',
href: 'http://google.com',
title: 'Become a Googler',
rel: 'external',
text: 'Go to Google!'
}).appendTo('#mySelector');
@unknownuser88
unknownuser88 / gist:8284844
Created January 6, 2014 15:54
2 random numbers
var r = (function() {
function sample_range(t,n) {
var l = [],r = [];
for (var i = 1; i <= t; i++) r.push(i);
for (var i = 0; i < n; i++) l.push(r.splice(Math.random() * r.length, 1));
return l;
}
@unknownuser88
unknownuser88 / gist:8284833
Created January 6, 2014 15:53
Using AJAX to test if the image exists
$("img").each(function() {
$.ajax({
url: $(this).attr('src'),
type: 'HEAD',
error: function() {
//image doesn't exist
console.log('ERROR');
},
success: function() {
//image exists
@unknownuser88
unknownuser88 / gist:8284809
Created January 6, 2014 15:52
Fix broken images using ajax
$(".productBoxImage img").each(function() {
var _this = $(this);
$.ajax({
url: $(this).attr('src'),
type: 'HEAD',
async: false,
error: function(e) {
if (e.status == '404') {
$(_this).attr('src', [replaceImageUrl]);
@unknownuser88
unknownuser88 / gist:8284797
Created January 6, 2014 15:51
6 JQUERY CURSOR FUNCTIONS
(function($, len, createRange, duplicate){
"use strict";
$.fn.caret = function(options,opt2) {
if ( typeof this[0] === 'undefined' || this.is(':hidden') || this.css('visibility') === 'hidden' ) { return this; }
var n, s, start, e, end, selRange, range, stored_range, te, val,
selection = document.selection, t = this[0], sTop = t.scrollTop,
ss = typeof t.selectionStart !== 'undefined';
if (typeof options === 'number' && typeof opt2 === 'number') {
start = options;
end = opt2;
@unknownuser88
unknownuser88 / gist:8284791
Created January 6, 2014 15:51
find in json
function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val));
} else if (i == key && obj[key] == val) {
objects.push(obj);
}
}
@unknownuser88
unknownuser88 / gist:8284780
Created January 6, 2014 15:50
string to title case
function toTitleCase(str){
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
@unknownuser88
unknownuser88 / gist:8284767
Created January 6, 2014 15:49
INPUT TEXT SELECTION CODE SNIPPETS
// GET CURSOR POSITION
jQuery.fn.getCursorPosition = function() {
if (this.lengh == 0) return -1;
return $(this).getSelectionStart();
}
//Set Text Selection
jQuery.fn.getSelectionStart = function() {
@unknownuser88
unknownuser88 / gist:8284756
Created January 6, 2014 15:49
$("img").error....
$(document).ready(function(){
$("img").error(function(){
$("img").replaceWith("<p>Error loading image!</p>");
});
$("button").click(function(){
$("img").error();
});
});
@unknownuser88
unknownuser88 / gist:8284752
Created January 6, 2014 15:49
my namespace template
;var d = {
author: 'David',
//variables available to whole of namespace
someIds: [],
options: {
width: 400,
height: 300,
title: 'title'