Flabby Rabbit | The offical home of The Rabbit

Firebug Lite in IE

Anyone who has tried to make their projects work on ALL browsers (including IE6 and 7) will know the headache that it can become. With no real developer tools available it can be incredibly difficult to trace and debug problems. When you start using Firebug in Firefox or Inspect Element in Chrome you quickly forgot how you ever survived before.

Here is a quick snippet that will add some firebug functionality to IE, just add this code to the page:

<!--[if IE]>
<script type="text/javascript" src="https://getfirebug.com/firebug-lite.js">
{
    overrideConsole: false,
    startInNewWindow: true,
    startOpened: true,
    enableTrace: true
}
</script>
<![endif]-->

External MySQL database with Node.js

Getting node to connect to a local database is a piece of cake, but when trying to connect externally I found very little on the internet … and ended up just guessing until it worked.

First you need to get nodes mysql plugin installed:

npm install mysql

“The mysql module is entirely written in Node.js, and implements an async MySQL client.” – nice introduction and the projects github. Here is my implementation, be warned it is horrible. Use the previous link to see how this should have been done:

var _mysql = require('mysql');
var MYSQL_HOST = '127.0.0.1';
var MYSQL_PORT = '3306';
var MYSQL_USER = 'user';
var MYSQL_PASS = 'pass';
var DATABASE = 'db_name';

var mysql = _mysql.createClient({
    user: MYSQL_USER,
    password: MYSQL_PASS,
    host: MYSQL_HOST,
    port: MYSQL_PORT,
});

mysql.query('use ' + DATABASE);

mysql.query('SELECT value FROM settings WHERE setting = "chan"', function selectCb(error, results, fields) {
  if (error) {
    console.log('Error getting settings: ' + error.message);
    return;
  }

  if(results.length > 0) {
    results.forEach(function(row) {
        console.log('Connecting to: ' + row['value']);
    });
  }
});

Drag and drop targets with jQuery



Final result:
http://www.bbc.co.uk/news/health-17235058

Multi line strings in Javascript

String concatenation is the most common method of formatting long strings in Javascript, but it isn’t the only or best way. For better performance try using backslashes to split up your lines.

Backslashes

var text = "I am a long string that I would\
like to split up on to multiple lines to keep my code\
nice and neat, which (should be) every developers aim."
;

String Concatenation

var text = "I am a long string that I would like to split "
         + "up on to multiple lines to keep my code nice and "
         + "neat, which (should be) every developers aim.";

Running a function after page execution

“Registers a callback to be executed after script execution finishes or exit() is called.” – php.net

Here is a simple class to show the execution time of a page:

<?php
    class logger {
       
        private $exec_start, $exec_time;
       
        function __construct() {
            $this->exec_start = microtime(true);
       
            register_shutdown_function(array($this, 'basic_log'));
        }
       
        public function basic_log() {
            $exec_time = microtime(true) - $this->exec_start;
            echo $exec_time;
        }
    }
?>

Irc

image

Quick and easy way to avoid CSS hacks

Probably the most incredibly useful snippet I have learnt recently. It’s beautifully simple and unbelievably essential. Creating websites for such a wide variety of platforms is hard enough but then when you throw IE6 and no script into the mix things can become very messy.

Replace the tag with the following code and most of the hard work will be taken care of:

<!--[if lt IE 7]><body class="no-js ie6"><![endif]-->
<!--[if IE 7]><body class="no-js ie7"><![endif]-->
<!--[if IE 8]><body class="no-js ie8"><![endif]-->
<!--[if gt IE 8]><!--><!--<![endif]-->
<script type="text/javascript">document.body.className = document.body.className.replace("no-js","js");</script>

Now you can easily target and correct any of the unique “features” of IE and also gracefully degrade when no javascript is detected.

A much better version of this post: Better conditional classnames for hack-free CSS

Final note: It might be a better idea to have no-js on the html tag instead of the body. The main reason for this is that if you wanted to test for no-js and ie6 you wouldn’t be able to with the above method. This is because IE does not support chained class selectors so you can’t do:

.no-js.ie6 div

Changing inc style permanently in Notepad++

Notepad++ by default interprets .inc files as being pascal source files. This by no means incorrect, it is just not very helpful when the .inc files I mainly work with are related to SSI and therefore are mostly HTML.

Text file containing declarations, headers, functions, or other data referenced by a program’s source code; can be used with C/C++, Pascal, Java, PHP (Web pages), and other languages.

The file you need to be interested in is located at %APPDATA%\Notepad++\langs.xml (e.g. C:\Documents and Settings\<username>\Application Data\Notepad++). In this file you need to remove inc from the ext of the Pascal node and add it to that of the html node.

<Language name="html" ext="html htm shtml shtm xhtml inc" commentLine="" commentStart="&lt;!--" commentEnd="--&gt;">;

Save this file, close Notepad++ and when you open a .inc file it should now be highlighted as if it was a HTML file.

Side note:
The langs.model.xml file in C:\Programs\Notepad++ is the factory default settings, and changing the declaration here will have no effect.

Deleting entire row based on column value

I needed to remove all rows in an excel sheet where column A was blank. With 4000 rows to sort through I thought I would attempt to write my first macro to do the job for me. I have no prior knowledge of VB so didn’t expect to get very far. Boom, done – all I can say is VB is horrible.

Sub deleteRows()
    Dim n As Long, rowCount As Long
    rowCount = Application.WorksheetFunction.CountA(Range("B:B"))
    For n = 1 To rowCount Step 1
        If Range("A" & n).Value = "" Then
            Rows(n).Delete
            If Range("B" & n).Value <> "" Then
                n = n - 1
            End If
        End If
    Next
End Sub

This solution works perfectly but I have just had an idea that might work. If you used a reverse loop from rowCount to 1 then there would be no need for the if statement; as the index of the rows to be processed will not be changed. I may update this post when/if I get a chance to have another look.

AOP/Mozilla Hack Day

HTML5 – News publishing stuff

Thursday 13 Oct:
Soho
Met up with people from a wide range of media houses. News Int, TIme Out, Nuts etc.. formed teams and got started making Will I Make It (on time).

Friday 14 Oct:
Westminster/London Eye

 

Essentials