Browse ALL the snippets ! - Page 5 of 6

Only one search for many applications

// You shouldn't use :
$(".bob").css("top","24px");// Search for bob, then css application
$(".bob").animate({top:0});// Search for bob, then animat application
$(".bob").hide();// Search for bob, then hide application

// But use instead :
$(".bob").css("top","24px").animate({top:0}).hide();// Search for bob, then do all those things with the same guy. Only one research

This little trick might be useful if you want your text to break at a specific point when handling it with media queries or something.

/** Notice the extra space to prevent the wrapping words from sticking to each other **/
<p>I want my text to break <br />right here.</p>

/** This will cancel the line-break **/
p br { display: none; }

/** This will restore it **/
p br { display: block; }

Ajout d'une méthode pour valider des expressions régulières pour jQuery validation.

    function(value, element, regexp) {
       var re = new RegExp(regexp);
       return this.optional(element) || re.test(value);
    "Please check your input."

This couple of simple URL rewriting rules will help you avoid some mistakes that are syntaxically legal and therefore might be referenced upon a bad backlink to your website or something.

# Multiple slashes
RewriteCond %{THE_REQUEST} \ (.*)//+(.*)\  [NC]
RewriteRule ^(.*)$ %1/%2 [L,R=301]
# Trailing dot on domain
RewriteCond %{HTTP_HOST} (.*)\.$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
# Trailing question mark
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^\ \?]*\?($|\ )
RewriteRule ^(.*)$ /$1? [L,R=301]
# Trailing slash(es)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1 [L,R=301]

If you're using Shadowbox and the likes, some of your elements' rel attributes might come with fancy values the W3C validator won't like. Put these values in data-rel instead, and use the following :

jQuery('[data-rel]').each(function() {
    var that = jQuery(this);
    that.attr('rel', function(i, val) { return ((typeof val === 'undefined') ? '' : val+' ')'rel'); }).removeAttr('data-rel');

Will return a substring from field_name, from the first occurence of needle to the end

SELECT SUBSTRING(field_name, instr(field_name, 'needle'), LENGTH(field_name)) AS result FROM table

This function has the behavior many of us dreamed of for its JS homonym !

function parseInt($s) {
    return ((int) preg_replace('~[^0-9]+~', '', $s));

This function allows you to switch between SQL (YYYY-MM-DD) and "human-readable" (DD/MM/YYYY) date formats at the speed of light. No calculations, only string manipulation !

function switchDF($date) {
    $s = array("-", "/");
    for ($i=0;$i<count($s);$i++) {
        $date = explode($s[$i], $date);
        if (count($date) == 3) return $date[2].$s[(int)!$i].$date[1].$s[(int)!$i].$date[0];
        else $date = $date[0];
    return false;

...which make them wrap <a> tags around it and gently break your design !

<meta name="format-detection" content="telephone=no" />

Pretty useful if you can't achieve your aims with pure portrait/landscape media queries !

window.onorientationchange = function()  {
    // Your code here