Skip to main content

Chris Coyier

Founder, writer, designer, spam-deleter, email personality

Snippet

Cross Domain GET Forwarding

When you do an AJAX request on a website, the URL you request from needs to reside on the same domain as where the request was made from. This is a security restriction imposed by the browser. There is a way to sneak around this by using a bit of a “man in the middle” approach.

PHP, being a server-side language, has the ability to pull content from any URL. So a PHP file can become the man in the … Read article “Cross Domain GET Forwarding”

Article

May is Maintenance Month

There are over 600 blog posts on this site over the course of nearly 3 years. Some of them are… better than others. I think it is the responsible thing to do is try to keep that content current and in good working order. So I’m going to dedicate a full month to it and declare May “Maintenance Month.” I have identified a good number of articles that I would like to go back and revisit. Some because technology has … Read article “May is Maintenance Month”

Snippet

Fallback for CDN hosted jQuery

Several big companies offer copies of jQuery hosted on their CDN’s (Content Delivery Network). Most notoriously Google, but also Microsoft and jQuery themselves. A lot of people swear by this since it saves bandwidth, downloads faster, and perhaps even stays cached jumping between different sites that use the same script.

There is always that twinge of doubt though, that perhaps something goes wrong with these big companies CDN at the script isn’t available (it happens). It’s more reliable to use … Read article “Fallback for CDN hosted jQuery”

Article

jQuery UI’s Position Function (plus, MagicZoom from scratch)

Knowing how to position things where you want them is one of the most important tools in a web designer’s toolbox. Understanding page flow, understanding everything is a box, understanding floats, etc. Once the basics are learned, a common question for intermediate designers is how to position elements relative to other elements. The standard CSS solution for this is to use the ol’ absolute positioning inside relative positioning trick. In a nutshell, you can use set relative positioning (or … Read article “jQuery UI’s Position Function (plus, MagicZoom from scratch)”

Article

My Stuff from jQuery Conference

The Bay Area jQuery Conference was amazingly fun. I met TONS of great people who I’ve wanted to meet forever. It was kinda like a Bluegrass Festival in some ways. The sessions weren’t recorded so unfortunately you can’t check them out that way. I’m going to embed my own slides below and you can view other slideshows from the conference on Slideshare or see pictures on Flickr. … Read article “My Stuff from jQuery Conference”

Snippet

iPad Detection

Of course, the iPad is a pretty large screen and a fully capable browser, so most websites don’t need to have iPad specific versions of them. But if you need to, you can detect for it with .htaccess

RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://ipad.yourdomain.com [R=301]

This will redirect iPad users to a URL you specify. This is probably the best way to do it (assuming you are running an Apache server), but if you aren’t, there are PHP and JavaScript Read article “iPad Detection”

Snippet

Facebook “Like” Button for WordPress

Some very easy copy-and-paste code here to add to the template for blog posts to allow for Facebook “liking” of the article. Probably best in the single.php template underneath where it outputs the content of the post.

<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo rawurlencode(get_permalink()); ?>&amp;layout=standard&amp;show-faces=true&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" id="facebook-like"></iframe>
Read article “Facebook “Like” Button for WordPress”
Snippet

Trigger Click on Input when Label is Clicked

Labels should have “for” attributes that match the ID of the input they are labeling. This means we can snag that attribute and use it in a selector to trigger a click on the input itself. Assuming of course you have some reason to watch for clicks on inputs.

var labelID;

$('label').click(function() {
       labelID = $(this).attr('for');
       $('#'+labelID).trigger('click');
});
Read article “Trigger Click on Input when Label is Clicked”
Snippet

Random Hex Color

Technique #1

<?php 
    
    $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
    $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
    
?>

Then echo out the $color value anywhere you need it. For example:

<body style="background: <?php echo $color; ?>;">

Technique #2

<?php printf( "#%06X\n", mt_rand( 0, 0xFFFFFF )); ?>

There is also a JavaScript version. … Read article “Random Hex Color”