Skip to content

Instantly share code, notes, and snippets.

@stenvdb
stenvdb / img-macros.twig
Last active July 4, 2022 10:49
Macro collection for Imager in Craft CMS
{#
# USAGE:
# (transform can either be an object, or an array of objects, aka srcset)
#
# {{ macro.regular(entry.image.one(), { width: 50, height: 50 }, 'class="block"') }}
# ➡️ <img src="..." width="50" height="50" alt="..." class="block" />
#
# {{ macro.url(entry.image.one(), { width: 50, height: 50 }) }}
# ➡️ /uploads_c/../{...}.jpg
#
@stenvdb
stenvdb / gist:2880975
Created June 6, 2012 09:42
JQuery: Check if image is in browser cache
alert("Expected: true or false\n" +
cached(big_img)
+ "\n\nExpected: false (cache-busting enabled)\n" +
cached(big_img + "?" + new Date().getTime()));
function cached(url){
var test = document.createElement("img");
test.src = url;
return test.complete || test.width+test.height > 0;
}
@stenvdb
stenvdb / gist:2157216
Created March 22, 2012 08:48
jQuery: Debug AJAX requests
$.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
}});
@stenvdb
stenvdb / gist:2114280
Created March 19, 2012 14:29
JavaScript: if keycode is character
var c= String.fromCharCode(event.keyCode);
var isWordcharacter = c.match(/\w/);
@stenvdb
stenvdb / gist:2107947
Created March 19, 2012 11:15
jQuery: Capitalize first letter of string
$.fn.capitalize = function () {
$.each(this, function () {
var caps = this.value;
caps = caps.charAt(0).toUpperCase() + caps.slice(1);
this.value = caps;
});
return this;
};
@stenvdb
stenvdb / gist:2044958
Created March 15, 2012 16:01
JavaScript: Init AddThis Ajax
// Init addthis
if (addThisFirstLoad) {
var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
if (window.addthis){
window.addthis = null;
}
$.getScript( script, function(){ addthis.init(); } );
addThisFirstLoad = false;
} else {
// Update the buttons (and dont download the script twice)
@stenvdb
stenvdb / gist:2031459
Created March 13, 2012 20:45
JavaScript: Open links new window
$("a[href^='http:']:not([href*='" + window.location.host + "'])").each(function() {
if( !$(this).hasClass('fancybox') )
{
$(this).click(function()
{
window.open(this.href); // pop a new window
return false; // return false to keep the actual link click from actuating
});
}
});