Thursday, December 20, 2007

The ultimate solution to blogging source code

Finally Mário Romano found the answer to the Ultimate Question of Life, the Universe, and Everything. Well... not quite but he found a great solution to the question "how do I post source code in my blog posts". Find it here:

pre {
  background-color:#FCFBF8;
  border-color:#CCCCCC;
  border-style:solid;
  border-width:1px 1px 1px 2px;
  line-height:1.2em;
  margin:1em auto;
  overflow:auto;
  padding:1em;
  width:90%;
  word-wrap:normal; /* IE Fix */
}

And by the way, the answer to the Ultimate Question of Life, the Universe, and Everything is 42.

Monday, November 19, 2007

How to exclude your own visits from Google Analytics and ClustrMaps

After a 5 minute Google (re)search I found a few ways to exclude my own visits to my sites/blog from the Google Analytics and ClustrMaps reports.

Google recommends two ways of doing this:

  • Creating a filter that excludes your IP address from the reports
  • Installing a cookie in your browser and creating a filter that excludes your visits from the reports

I don't find the first option very useful because I don't use static IP addresses.

As for the other option, I really didn't try that. Google doesn't explain very well how to do it and I didn't have the patience to explore that option.

Another downside of both of these options is that they require that you edit your html. OK, that's not really a problem but when you're managing a blog, you don't want to mess with your HTML too much.

I found an easier approach in here:

Use AdBlock Plus to block communication with Google Analytics and ClustrMaps.

These are the addresses I had to block:

  • http://www.google-analytics.com/urchin.js
  • http://www3.clustrmaps.com/counter/index2.php?url=http://(enter your address here)

Don't forget to disable the Collapse blocked elements option in AdBlock. If you don't disable that option, you won't be able to see the link to you ClustrMap (Adblock simply doesn't show anything).

[Update]

Ok, there's a problem with the approach I described for Google Analytics: It will block the analytics scripts of other sites you open (including your friends' blogs).

To fix this you have to... well, change the HTML of your site :-D

In your HTML find the following tag:

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">

and replace with:

<script src="http://www.google-analytics.com/urchin.js#url=http://(replace with your site url)" type="text/javascript">

I added the #url=http:(replace with your site url) which in fact doesn't do anything when getting the script from the server but allows us to setup AdBlock to block this URL.

Next, you have to setup AdBlock to block only http://www.google-analytics.com/urchin.js#url=http://(replace with your site url) instead of the late http://www.google-analytics.com/urchin.js.

Thursday, November 8, 2007

input image objects not found in form.elements

Imagine you write the following javascript sentence in an html page:

self.document.forms[0].elements

That will give you all the elements inside the first form (forms[0]) in the current page, right? Well, no. For some reason, if you have an input of type image (<input type="image">) in the form, that element will not be in the forms[0].elements object.

Every other input (button, text, select, submit, ...) will be in the elements object, but do not expect to find image type inputs. I've tried that in IE and Firefox unsuccessfully.

(If you don't know what an input of type image is, well it's behaviour is the same as the submit inputs but instead of a button, the user is presented with an image.)

So, how do we solve this? Well, instead of using:

self.document.forms[0].elements

we have to use the following:

self.document.all

Of course, that way, you will get a list of all the elements in the current page instead of all the elements in a form but you will find the inputs of type image in there, for certain.

Wednesday, November 7, 2007

Buying a new PC

I will be buying a new desktop PC soon, so I'm starting to study what components are out there in the moment, their features, prices, etc.

This is the (short) list of the sites I'm reading to get this kind of information. I will be updating the list as I get to know more interesting sites:

Wednesday, October 31, 2007

The Pac-Man joke

I just love this joke by the comedian Marcus Brigstocke:

If Pac-Man had affected us as kids, we'd all be running around in dark rooms, munching pills and listening to repetitive electronic music.

It has been often attributed to several famous figures in electronics. Usually it is said that Kristian Wilson, Nintendo's CEO, wrote it in 1989. But it is in fact Brigstocke's joke and he claims that:

(...) it is very unlikely that it was written by a Nintendo employee in 1989, being as Pac Man was still around and not much of a childhood memory (...)

Thursday, October 18, 2007

Use Wildcard Characters as Literal Characters (in LIKE comparisons)

We often use SQL queries with LIKE comparisons. When we do so, we usually use the % wildcard character. For instance, if i want to obtain every book with the word SQL in its title, in a Books database table, I would write something like:

SELECT * FROM Books WHERE Title LIKE '%SQL%'

You can also use the other wildcard characters/expressions: _ (underscore), [] and [^].

The problem comes when you need to find strings that contain these wildcard characters themselves. For instance, imagine you're searching a list of CD's and you want to find all CD's that contain the string [SOUNDTRACK]. As you can see you're searching a string that contains the wildcard characters [ and ].

You want those characters to be considered literal characters instead of wildcard characters, so you have enclose them in brackets, like so:

[[]SOUNDTRACK]

The ] character, for some reason, doesn't need to be enclosed in brackets but all other wildcard characters do: [, _ and %

So if you're going to run a LIKE comparison with a string that can contain wildcard characters and you want them to be considered like literal characters you can use the following trick (i'm using the soundtrack example):

DECLARE @str AS VARCHAR(255)
SET @str = '[SOUNDTRACK]'

SET @str = replace(@str, '[', '[[]')
SET @str = replace(@str, '_', '[_]')
SET @str = replace(@str, '%', '[%]')

SELECT * FROM CDs WHERE NAME LIKE '%' + @str + '%'

You can also use the ESCAPE clause. If you want to know more about LIKE comparisons, check here.

Friday, October 12, 2007

How to "clear screen" in javascript

I wanted to clear an HTML page after clicking a button. So how do I do it in javascript? I wanted something simple like the "cls" command that exists in several programming languages (including the "cls" command in the windows command prompt).

After a simple google search I found two options that I liked in the thescripts.com forum:

location.replace("about:blank")

or

document.body.innerHTML=""

I prefer the second option because I can insert what I want in the innerHTML property.