Skip to content

Instantly share code, notes, and snippets.

View jhammann's full-sized avatar
👐

Jeroen Hammann jhammann

👐
View GitHub Profile
@jhammann
jhammann / social_plugins.css
Created March 12, 2013 16:31
When you put the 'like' and 'tweet' social plugins next to each other the 'tweet' button's position is always a few pixels lower than the 'like' button. This is a simple fix for that annoying occurrence using the iframe option for Facebook's social plugin.
.social_plugins iframe {vertical-align:top;}
@jhammann
jhammann / text_length_helper.rb
Last active December 16, 2015 01:18
A helper that checks the length of a string and which adds three dots if that string exceeds a given length.
module TextLengthHelper
def text_length(string, max_length)
return string if string.length <= max_length
string[0..(max_length - 1)] + "..."
end
end
@jhammann
jhammann / checked.js
Created April 25, 2013 13:20
A simple way of showing or hiding something like an alternate shipping address. Use this when you have a checkbox, link or something else 'clickable'
$('#alt_shipping_address').click(function() {
if( $(this).is(':checked')) {
$(".shippingContainer").show();
} else {
$(".shippingContainer").hide();
}
});
@jhammann
jhammann / mobile_check.rb
Created April 25, 2013 13:21
Ruby code check for mobile devices. The first check covers all mobile devices but with the second one you can be more specific. For instance if you don't want support for iPad's you can just remove 'iPad'. Too bad the same can't be done for Android devices, you cannot exclude tablets or other devices.
# simple mobile check which covers all devices.
def is_mobile?
request.user_agent =~ /Mobile|webOS/
end
# more advanced mobile check, you can exclude devices.
def is_mobile?
request.env["HTTP_USER_AGENT"][/(iPhone|iPad|iPod|BlackBerry|Android)/]
end
@jhammann
jhammann / toggle_text.js
Created May 17, 2013 14:27
A simple click function that toggles a hidden text and changes the text of the link you'll have to click.
$(".showMoreIntroText").click(function(){
$(".hiddenIntroText").slideToggle();
$(this).text($(this).text() == "Read more..." ? "Read less..." : "Read more...");
});
@jhammann
jhammann / focus_on_input.js
Created June 3, 2013 11:24
A javascript function to focus on an input field when a checkbox or radio button is changed (checked).
$('input#radio').change(function(){
$("input#amountInput").focus();
});
@jhammann
jhammann / mobile_check.js
Created June 25, 2013 13:58
Javascript code for checking if a user is on a mobile device. Mobile devices also include tablets. Remove 'iPad' if you do not want to include the iPad. Android serves all Android devices so you can't exclude a certain type.
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
// code here
} else {
// code here
}
@jhammann
jhammann / keydown_toggle.js
Created June 27, 2013 19:19
I created a snippet for showing an element (a link to an edit form, hence the element classname) on keydown.
$("body").keydown(function(e){
if (e.keyCode == 69) {
$('.editLink').toggle();
}
});
@jhammann
jhammann / post.rb
Created June 28, 2013 15:11
Keep forgetting this useful method for creating permalink. Thank god for gists. Put this inside a model.
def to_param
"#{id}-#{title.parameterize}"
end
@jhammann
jhammann / check_localstorage.js
Created July 15, 2013 15:06
Paste this javascript line into Chrome's console to check how much size your current local storage takes up.
// paste this in your chrome's console
for(var x in localStorage)console.log(x+"="+((localStorage[x].length * 2)/1024/1024).toFixed(2)+" MB");