Common reasons for failing and unexpected behavior in jquery for internet explorer

After spending the best part of 2 days debugging a large jquery validation script, the most common problems I had to solve were:

Clash of JSON object names and arbitrary variables

Yes folks, your JSON object may be:

var foo = {
    bar: {
        blah: "blahdy blah",
   }
}

But try and use:

var blah = "diddly dee";

Even in an isolated function and internet explorer will cry like a bitch.

Misuse of jQuery DOM element building

You have to be much stricter with elements in IE.

elem1 = $("div");
//INCORRECT
elem2 = elem1
    .append("div");

//ALSO INCORRECT
elem2 = elem1
    .append($("<div>"))
//CORRECT elem2 = elem1 .append($("<div/>"));
Posted in IE | Leave a comment

SASS without RUBY – a year on. New MIXINS and code examples.

Hello lovelies,
I’ve been using SASS for about a year now, and although I don’t get the chance to use it as much as I’d like – I’d thought I’d share with you my progress.

You can just download the code samples here – they are from a real life corporate website!

mixins.scss
main.scss

Using fake classes with @extend

I only just discovered @extend and at first I didn’t find it that useful until I made FAKE CLASSES!

$ff : 'Trebuchet MS', Garuda, sans-serif;
//font simple
@mixin fs($size, $col: inherit, $lh: $size) {
    color: $col;
    font: normal #{$size}/#{$lh} $ff;
}
//font simple bold
@mixin fsb($size, $col, $lh: $size) {
    color: $col;
    font: bold #{$size}/#{$lh} $ff;
}
 
.h1 {
    @include fsb(28px, #333);
}
 
.h2 {
    @include fsb(16px, #333, 21px);
}
 
.h2-larger {
    @include fsb(18px, #414141, 21px);
}
 
.h3 {
    @include fs(14px, #333, 24px);
}
 
h3, p, input, li {
    @extend .h3;
}
/////
#main {
    form {
        header {
            @extend .h2
            padding-bottom: 2px;
            ////
        }
    }
}

The reason we use fake classes rather than the actual selectors is becuase extend will extend EVERY version of say, an ‘a’ tag so if we use fake classes extend will only copy the classes for one context.

So you can basically have ALL of your site styles declared as fake classes at the top of your SASS stylesheet then extend away throughout the rest of it – This means your heading styles or whatever can be completely consistent in every context. Really loving this.

Many mixins make light work

When I first started with mixins I really over-complicated the whole process by trying to have as few mixins as possible. What happened in the end was that my SASS @include code was too complicated and I was actually having to look up my own syntax.

Of course there’s absolutely no benefit to trimming down your mixin code – the bloody CSS is pre-compliled anyway! It turns out you’re wayyy better off having lots of little small mixins rather than a few gigantic ones. LET ME ILLUSTRATE MY POINT.

My old ‘position’ mixin was a real beast that covered absolute, relative, top, left, right, bottom. You can see how I incorporated a pading/margin style shorthand.

//'i' is for 'inherit'
@mixin pos($p: relative, $t: 0, $b: i, $r: i, $l: 0) {
    @if $b!=i { bottom: #{$b}px; }
    @if $l!=i { left: #{$l}px; }
    @if $p!=i {
        @if $p==a {
            position: absolute;
        } @else {
            position: relative;
        }
    }
    @if $r!=i { left: #{$r}px; }
    @if $t!=i { top: #{$t}px; }
}

#div1 {
    @include pos;
    border: 1px solid green;
    height: 500;
    width: 500;
    div {
        @include pos(a);
        border: 1px solid red;
        width: 100px;
        height: 100px;
	#div2 {
	    @include pos(i,100,i,i,500);
	}
	#div3 {
	    @include pos(i,i,50,50,i);
	}
	#div4 {
	    @include pos(i,i,i,200,200);
	}
    }
}

I’m kind of quite proud of it and it works well but it’s just a bit clunky somehow and you can’t really see what’s going on right away, you have to think about it. That’s when I decided to break it up in to 4 mixins:

//position top left
@mixin ptl($p: relative, $t: 0, $l: 0) {
    @if $l!=i {left: #{$l}px;}
    @if $p==a {
        position: absolute;
    } @elseif $p==i {
    } @else {
        position: relative;
    }
    @if $t!=i {top: #{$t}px;}
}

//position bottom right
@mixin pbr($p: relative, $b: 0, $r: 0) {
    @if $b!=i {bottom: #{$b}px;}
    @if $p==a {
        position: absolute;
    } @elseif $p==i {
    } @else {
        position: relative;
    }
    @if $r!=i {right: #{$r}px;}
}

//position top right
@mixin ptr($p: relative, $t: 0, $r: 0) {
    @if $p==a {
        position: absolute;
    @elseif $p==i {
    } @else {
        position: relative;
    }
    @if $r!=i {right: #{$r}px;}
    @if $t!=i {top: #{$t}px;}
}
//bottom left hasn't come up yet!

#div1 {
    @include ptl;
    border: 1px solid green;
    height: 500;
    width: 500;
    div {
        @include ptl(a);
        border: 1px solid red;
        width: 100px;
        height: 100px;
	#div2 {
	    @include ptl(i,100,500);
	}
	#div3 {
	    @include pbr(i,50,50);
	}
	#div4 {
	    @include pbl(i,200,200);
	}
    }
}

And although it only looks like a slight difference, it means I just have a much better grip of what is going on in my code & don’t have to think so much.

Posted in sass | Leave a comment

[solved] Installing a printer on my Virtualbox Windows 7 x64 guest (hint: print spooler service)

Well that was 3 hours I’ll never get back.

For whatever reason (possibly software piracy) the ‘Print Spooler’ service was disabled in my Windows services (Thanks for telling me Mr. Windoze). I simply enabled it with Task-manager -> Services -> [Services...] find Print Spooler, change the startup type to ‘automatic’ and press Start. Then detach + re-attach your printer cable (either virtually or otherwise)

Sorted, respect due.

Posted in virtualbox | Leave a comment

How and why I use Dropbox to sync my localhost and staging server

Drobox really has changed my life – for the better. I have a folder called ‘web’ that contains all of my projects – I use local apache to serve it to my local machine.

I’m lucky enough to have the awesome slicehost as my hosting provider, meaning it was a cinch to install dropbox on my remote server by following these instructions http://wiki.dropbox.com/TipsAndTricks/TextBasedLinuxInstall This way my localhost is perfectly in sync with my staging server – as long as I update both my local and remote hosts & httpd.conf files appropriately.

The only downside is that any local mistakes I make go right in the clients face – so perhaps best to turn it off if you’re doing anything fancy.

The upsides are amazing though – no FTPing + dropbox is completely versioned in an intuitive way though their web interface, no subversion needed EVER AGAIN! Not to mention if I need someone else to work on stuff I can just share that folder with them directly.

Eithinkyou.

Posted in Uncategorized | 1 Comment

Add your local CSS to a live server you do not have access to with MyLive

This is a plugin I made a while ago when I was working at the BBC. Their dev environment was completely different to the live one so we never really got the impact our CSS changes would make until they were put live (which took hours).

It essentially swaps your named local CSS with named live CSS and as it’s a bookmarklet it’s completely browser independent. It can be however, a little crashy in IE6 but then – what isn’t?

The bookmark creator and all instructions live at accessibledesign.net/mylive

Posted in Uncategorized | Leave a comment

Accessing multiple locally hosted Apache sites from local virtual machines

Well BLIMEY! Despite there being hundreds of guides all over google about this very subject I have NEVER managed to get this working until TONIGHT.

My current setup is Ubuntu host running Windows 7 guest on virtualbox – however as this is an Apache/hosts issue you should be able to get it working on whatever setup providing you change these steps accordingly.

  1. Add a host-only network adapter to your guest machine. in Virtualbox this is done by (while the machine is off) Selecting Details -> Network (select an unused network tab) And add it here, make a note of the Name, mine is vboxnet0
  2. In the terminal on your host machine type ifconfig and note the ip address next to vboxnet0 (or whatever yours is called)
  3. In the hosts files of guest and host add:
    123.456.789.012 localurl Where 123.456.789.012 is the IP Address from before and localurl is a new local site URL that will only be used for cross-virtual-machine browsing This is where I have fallen down in the past!! You’ll need a whole new virtual host name for this type  of connection even though the sites are hosted from the same folder.
  4. Edit your guest Apache httpd.conf file and add an entry for your new server right after the existing alias.
    NameVirtualHost existingsitealias
    
    <VirtualHost existingsitealias>
    ServerName existingsitealias
    DocumentRoot /path/to/site/root
    </VirtualHost>
    
    NameVirtualHost newsitealias
    
    <VirtualHost newsitealias>
    ServerName newsitealias
    DocumentRoot /path/to/site/root
    </VirtualHost>

    I have old-style Apache but if you have that new one with a2ensite you’ll need to add this as a new site the usual way.

And you can just keep on going like this repeating steps 3 & 4 as necessary.

Posted in hosting | Tagged , , | Leave a comment

Easy SASS for wordpress using phamlp

Hi there,

I’ve been getting quite into this SASS malarky so wanted to get it running on a new WordPress site I am developing. There are a few solutions out there using compass – eg Sass up your WordPress themes with Compass but installing this requires you to have sudo shell access to your server, install ruby and all that jazz. On most of the sites I work on the client doesn’t have that sort of access, let alone me.

The steps

  1. Get phamlp and extract it to the root of your wordpress site
  2. In your themes header.php shove this lot in in <head>:
    <?php $sass = new SassParser(array('style'=>'nested'));
    $file = $_SERVER["DOCUMENT_ROOT"].'/wp-content/themes/yourtheme/main.scss';//CHECK YOUR PATHS
    $css = $sass->toCss($file);
    echo $css;
    ?>

    Wrap in style tags – these have been omitted due to wordpress not liking them.

    And that will basically spit your compiled CSS – depending on your setup you may want to delete references to the default stylesheet.

Get PHAMLP to create a .css file

If you want to go one step further & get phamlp to create a CSS file for you – you’ll need this handy PHP function I have written. You will also need write permissions to wherever the css file is stored & your phamlp/sass folder – just try it anyway.

Steps

  1. create a blank css file & upload it to the server – this will be populated with the rendered sass
  2. Put this code at the end of your THEME/functions.php page:
    $troot = $_SERVER["DOCUMENT_ROOT"].'/wp-content/themes/yourtheme/'; //YOUR THEME ROOT
     
    //Convert .scss to css
    $fcss = $troot."css/main.css"; //CHECK PATHS!
    $fscss = $troot.'css/main.scss'; //CHECK PATHS!
     
    $fh = fopen($fcss, 'w') or die("can't open file");
    $sass = new SassParser(array('style'=>'nested'));
    $css = $sass->toCss($fscss);
    fwrite($fh, $css);
     
    fclose($fh);

Now you’ll need to amend your header.php to add the reference to this new file (the CSS one not the SCSS), and delete the reference to the old one if this supersedes it.

Posted in sass, wordpress | 5 Comments

Useful SASS mixins code examples

Font size shorthand

$default-font: Helvetica, Arial, Sans-serif;
$base-font: 16; 
 
@mixin f($font-size, $base: $base-font, $weight: normal, $line-height: $font-size, $font-family: $default-font) {  
    font: $weight #{$font-size/$base}em/#{(($line-height/$font-size)*100)}% $font-family;
}

Usage:

//@include f([font-size in px], [base font size], [font-weight], [line-height in px], [font-family])
 
h1 {
    @include f(24); //Output: font: normal 1.5em/100% Helvetica, Arial, Sans-serif;
}
 
h2 {
    @include f(24, 16, bold, 30, "Courier new, monospace"); //Output: font: bold 1.5em/125% "Courier new, monospace";
}

Background image

$iroot = \/wp-content\/themes\/theme\/images\/; //CHECK YOUR PATHS!
 
@mixin bg($file, $bgcol: transparent, $pos: left top, $rep: no-repeat, $ext: .png) {   
    background: $bgcol url(#{$iroot}#{$file}#{$ext}) $rep $pos; 
}

Usage:

@include bg(logo); //Output background: transparent url(/wp-content/themes/theme/images/logo.png) no-repeat left top;
 
@include bg(logo, #fff); //Output background: #fff url(/wp-content/themes/theme/images/logo.png) no-repeat left top;
 
@include bg(furniture/logo, #fff, 2px center, no-repeat, .jpg); //Output background: #fff url(/wp-content/themes/theme/images/furniture/logo.jpg) no-repeat 2px center;
Posted in sass | Tagged | 2 Comments

Getting started with SASS and HAML in Windows – a front end developers guide.

I am one lazy lady – so when I heard there was a way I type less stuff in my day to day job I jumped at the chance. I realise I’m a bit late on the SASS/HAML train, but baby – that’s how I roll.

SASS & HAML are kind of what jQuery is to Javascript. SASS being a framework for CSS and HAML – XHTML. We are only going to be using these tecnologies to create plain ol’ XHTML & CSS so we don’t need to worry about getting all Rubied up (not to a great extent anyway)

Requirements (download & install these)

  1. RUBY
  2. SASS & HAML (we’ll install this in a bit)

All installed? Sweet.

Getting started with SASS

Of course the SASS tutorial is an excellent place to start, however not being a Ruby developer it took me a little while to figure out how it all fits together in Windows – here’s the lowdown:

  1. You will be commonly entering commands in the ‘Start command prompt with Ruby’ application located wherever Ruby was installed on your PC – so run that now and input
    gem install haml

    Thanks to Mikkel Ricky below for letting me Know Sass & HAML have now been split so you’ll also need:

    gem install sass

    this will enable the SASS & HAML parsing functionality

  2. SASS has a handy function that will watch an entire folder for changes to .scss files and automatically create .css files.
    sass --watch stylesheets/sass:stylesheets/compiled

    A more Windowsy example:

    sass --watch "D:/Docs/dropbox/web/lr/scss":"D:/Docs/dropbox/web/lr/css"

    This command will watch the folder D:\web\example-site\scss for changes to any .scss files and automatically create a .css file with the same name in D:\web\example-site\css

  3. I went one step further and created a batch file that starts the Ruby command prompt and then automatically runs the watch command. Well – I say created I just copied the existing Ruby command prompt batch and added a load of watch commands to the end.
    @ECHO OFF
    REM Determine where is RUBY_BIN (where this script is)
    PUSHD %~dp0.
    SET RUBY_BIN=%CD%
    POPD
    
    REM Add RUBY_BIN to the PATH
    REM RUBY_BIN takes higher priority to avoid other tools
    REM conflict with our own (mainly the DevKit)
    SET PATH=%RUBY_BIN%;%PATH%
    SET RUBY_BIN=
    
    REM Display Ruby version
    ruby.exe -v
    
    REM Run SASS
    sass --watch "D:/Docs/dropbox/web/lr/scss":"D:/Docs/dropbox/web/lr/css"

    Download this script
    Just put the above code in a text file, edit the sass lines, and save it as whatever.bat somewhere nice.

    Thanks to commenter Jason for fixing my SASS folder watching code :)

Now it's time to learn the language a bit. After you have done that, come back here and check out my SASS mixins - you'll probably know what mixins are by then as well.

Getting started with HAML

HAML was a bit of a bugger to get going, but luckily you can learn by my mistakes. For some reason HAML doesn't have a built in watcher command. I spent ages trying to find a Ruby script that would work and the closest I found is this one by Blake Smith. It seemed to screw up windows folders but after a bit of trial and error I came up with this:

HAML watcher for Windows

# Script to watch a directory for any changes to a haml file
# and compile it.
#
# USAGE: ruby haml_watch.rb
#
require 'rubygems'
require 'fssm'
 
directory = ARGV.first
FSSM.monitor(directory, '**/*.haml') do
  update do |base, relative|
    input = "#{relative}"
    output = "#{relative.gsub!('.haml', '.html')}"
    command = "haml #{input} #{output}"
    %x{#{command}}
    puts "Regenerated #{input} to #{output}"
  end
end

Download this script
You'll need to install the 'fssm' gem from the Ruby command prompt:

gem install fssm

Example: (run from Ruby command prompt)

haml_watch.rb d:\web\site1

Also, make sure none of your site folder names have spaces in them! I learnt this the hard way as I keep all my sites in my Dropbox folder which is brilliantly named 'My Dropbox' It wasn't even that easy to rename! Had to use pyDropboxPath.

Forcing HAML to use double quotes

Fix found here Which basically amounts to text-editing engine.rb which I found at C:\Ruby192\lib\ruby\gems\1.9.1\gems\haml-3.0.18\lib\haml and changing the line:

:attr_wrapper =&gt; "'",

to

:attr_wrapper =&gt; "\"",

Including HAML in HAML

If anyone has a neater way of doing this please email me. Please note your path will be relative to the page you are including from.

= ::Haml::Engine.new(File.read(File.dirname(__FILE__) + '/pathto/file.haml'), instance_variable_get(:@haml_buffer).options).render

Note that HAML will automatically close tags so you can't have half tags in say a header include. EG if you want your header to be

!!!
%html
    %head
        %meta{'http-equiv' => 'Content-Type', :content => 'text/html; charset=UTF-8'}
        %title Lutyens &amp; Rubinstein   
        %link{:rel => 'stylesheet', :type => 'text/css', :href => '/css/meyer-reset.css'}
        %link{:rel => 'stylesheet', :type => 'text/css', :href => '/css/main.css'}
 
    %body
        %div#wrapper
            %div#content

That ain't gonna work as html, body, #wrapper, #content will all be closed off before you include it. Don't worry though as you can mix HAML with plain HTML like so:

!!!
<html>
    %head
        %meta{'http-equiv' => 'Content-Type', :content => 'text/html; charset=UTF-8'}
        %title Lutyens &amp; Rubinstein   
        %link{:rel => 'stylesheet', :type => 'text/css', :href => '/css/meyer-reset.css'}
        %link{:rel => 'stylesheet', :type => 'text/css', :href => '/css/main.css'}
 
    <body>
        <div id="wrapper">
            <div id="content">

bare in mind that HAML seems to cache its includes - I am looking into how to get around this.

Posted in haml, sass | Tagged , | 9 Comments