Skip to content

Instantly share code, notes, and snippets.

@quawn
quawn / content-loader.js
Last active January 4, 2016 03:19
JS: Load external content
$("#content").load("somefile.html", function(response, status, xhr) {
// error handling
if(status == "error") {
$("#content").html("An error occured: " + xhr.status + " " + xhr.statusText);
}
});
@quawn
quawn / zebra.js
Last active January 4, 2016 03:19
JS: Table Stripes (Zebra)
$(document).ready(function(){
$("table tr:even").addClass('stripe');
});
@quawn
quawn / page-refresher.js
Last active January 4, 2016 03:19
JS: Partial page refresh
setInterval(function() {
$("#refresh").load(location.href+" #refresh>*","");
}, 10000); // milliseconds to wait
@quawn
quawn / gist:8560663
Last active March 10, 2017 15:03
JS: Preload images
$.preloadImages = function() {
for(var i = 0; i<arguments.length; i++) {
$("<img />").attr("src", arguments[i]);
}
}
$(document).ready(function() {
$.preloadImages("hoverimage1.jpg","hoverimage2.jpg");
});
@quawn
quawn / ext-link-opener.js
Last active January 4, 2016 03:19
JS: Open external links in a new tab/window
@quawn
quawn / div-viewport.js
Last active January 4, 2016 03:19
JS: Div full Width/Height of viewport with jQuery
// global vars
var winWidth = $(window).width();
var winHeight = $(window).height();
// set initial div height / width
$('div').css({
'width': winWidth,
'height': winHeight,
});
@quawn
quawn / form.html
Last active January 4, 2016 03:19
JS: Test password strength
<input type="password" name="pass" id="pass" />
<span id="passstrength"></span>
@quawn
quawn / img-resizer.js
Last active January 4, 2016 03:19
JS: Image resizing using jQuery
$(window).bind("load", function() {
// IMAGE RESIZE
$('#product_cat_list img').each(function() {
var maxWidth = 120;
var maxHeight = 120;
var ratio = 0;
var width = $(this).width();
var height = $(this).height();
if(width > maxWidth){
@quawn
quawn / load-on-scrolling.js
Last active November 23, 2022 11:58
JS: Automatically load content on scroll
var loading = false;
$(window).scroll(function(){
if((($(window).scrollTop()+$(window).height())+250)>=$(document).height()){
if(loading == false){
loading = true;
$('#loadingbar').css("display","block");
$.get("load.php?start="+$('#loaded_max').val(), function(loaded){
$('body').append(loaded);
$('#loaded_max').val(parseInt($('#loaded_max').val())+50);
$('#loadingbar').css("display","none");
@quawn
quawn / img-loaded-checker.js
Last active January 4, 2016 03:19
JS: Check if an image is loaded
var imgsrc = 'img/image1.png';
$('<img/>').load(function () {
alert('image loaded');
}).error(function () {
alert('error loading image');
}).attr('src', imgsrc);