cjhaas.com Basically a place that Chris can post solutions to problems so he can easily find them later

July 23, 2009

Zero-Width HTML Space

Filed under: Uncategorized — Tags: , — Chris Haas @ 2:58 pm

Let’s say you have the text “Life insurance/Dependent Life” and you want to fit it into a small space with a small font. If you try it normally, most browsers might break the text so that it looks like:

Life
insurance/Dependent
Life

One solution is to just replace the forward slashes with a forward slash followed by a space giving you “Life insurance/ Dependent Life“. Not the end of the world but not ideal, I’d rather it break at the slashes. That’s where the zero-width space character (U+200B) comes in. You can replace the forward slash with a forward slash followed by and if the browser needs to, it will break at the forward slash, otherwise it will display the slash with no white space around it.

The bad news is that IE6 doesn’t know what to do with that character. The good news is that with a little javascript you can replace that character (and the even better news, after August 22nd hopefully most people will be upgraded to IE8). In my case, only my hyperlinks use this trick so below I’m searching for only <a> tags.


    if (navigator.userAgent.indexOf('MSIE 6.') > 0) {
        var as = document.getElementsByTagName('a');
        var p = /u200b/g;
        for (var i = as.length - 1; i >=0 ; i--) {
            if (p.test(as[i].innerHTML)) {
                as[i].innerHTML = as[i].innerHTML.replace(p, '<wbr/>');
            }
        }
    }

You may notice that I’m walking backwards through the array (and that I don’t like --i), and to be honest I’m not sure why but going forwards didn’t catch all of the occurrences. Also, in case you’re wondering, I use this in my L3 navigation so I don’t have to worry about monkeying with my web.sitemap file figuring exactly how to break things. It does cause a little jump to occur in IE6 because I’m calling the JS onload but that’s okay with me.

April 10, 2009

How to add metadata to Facebook link

Filed under: Uncategorized — Tags: , — Chris Haas @ 9:22 pm

When you post a link in Facebook, Facebook scans the page that you’re posting and looks for certain tags to tell it what the links is about. Unless you’ve setup your site with special markup, however, Facebook will just grab the page’s title and the first paragraph or two of the pages content, and possibly a random image. If you want to control this you need to add special markup.

Below is a sample of how to post a video:

<meta name="title" content="Neil Michelin's Huge Muskie" />
<meta name="description" content="Neil talks about catching his personal best, a fish nearing the 60&quot; mark, while Muskie fishing August second, 2008 on Lac Seul tossing a Shumway Flasher." />
<link rel="image_src" href="http://video.outdoorsfirst.com/159c6604-219b-441a-be7c-c86f56a4fe2c.wmv.jpg" />
<link rel="video_src" href="http://upload.outdoorsfirst.com/flash/video2.2_med.swf?ad=http://video.outdoorsfirst.com/slug_toyota.flv&amp;atitle=Advertisement&amp;video=http://video.outdoorsfirst.com/159c6604-219b-441a-be7c-c86f56a4fe2c.wmv.flv&amp;btitle=Neil Michelin's Huge Muskie&amp;ref=fb"/>
<meta name="video_height" content="387" />
<meta name="video_width" content="486" />
<meta name="video_type" content="application/x-shockwave-flash" />

Powered by WordPress