dannytalk ™ A Danny Ng Blog

Danny Ng blogs about SEO, Web Development, Christianity, and Life in General

Archive for August, 2008

How to handle multiple window events in Javascript

Posted by danny On August - 31 - 2008

I’ve written a window event handler mainly for handling multiple onload events. The typical way of handling multiple onload events is by creating a helper function that contains all the onload functions and using window.onload to call the helper function.

An example would look something like this,

function onloader()
{
    onload1();
    onload2();
    // and so on...
}
window.onload = onloader;

However, I wanted to write something more object oriented and more elegant. So I wrote my own method which will allow you to add multiple events to the window object. This script is very early in its stage and only allows you to add events. I will probably work on it more if I figure out more uses for it.

So here’s the script.

/**
 *	@author:	Danny Ng (http://www.dannytalk.com)
 *	@date:		30/08/08
 *	@notes:		Free to use and distribute without altering this notice. Would appreciate a link back.
 */

/**
 *	Window Events:
 *		- onblur
 *		- onerror
 *		- onfocus
 *		- onload
 *		- onresize
 *		- onunload
 *
 *	@usage: window.addEvent(event_type, function)
 *	@params: string, function
 */
function addEvent (e, func)
{
	var old_event;
	switch(e)
	{
		case 'onblur':
			old_event = this.onblur;
			if (typeof this.onblur == 'undefined')
			{
				this.onblur = function(e) {
					func();
				};
			}
			else if (typeof this.onblur == 'function')
			{
				this.onblur = function(e) {
					old_event();
					func();
				};
			}
			break;
		case 'onerror':
			old_event = this.onerror;
			if (typeof this.onerror == 'undefined')
			{
				this.onerror = function(e) {
					func();
				};
			}
			else if (typeof this.onerror == 'function')
			{
				this.onerror = function(e) {
					old_event();
					func();
				};
			}
			break;
		case 'onfocus':
			old_event = this.onfocus;
			if (typeof this.onfocus == 'undefined')
			{
				this.onfocus = function(e) {
					func();
				};
			}
			else if (typeof this.onfocus == 'function')
			{
				this.onfocus = function(e) {
					old_event();
					func();
				};
			}
			break;
		case 'onload':
			old_event = this.onload;
			if (typeof this.onload == 'undefined')
			{
				this.onload = function(e) {
					func();
				};
			}
			else if (typeof this.onload == 'function')
			{
				this.onload = function(e) {
					old_event();
					func();
				};
			}
			break;
		case 'onresize':
			old_event = this.onresize;
			if (typeof this.onresize == 'undefined')
			{
				this.onresize = function(e) {
					func();
				};
			}
			else if (typeof this.onresize == 'function')
			{
				this.onresize = function(e) {
					old_event();
					func();
				};
			}
			break;
		case 'onunload':
			old_event = this.onunload;
			if (typeof this.onunload == 'undefined')
			{
				this.onunload = function(e) {
					func();
				};
			}
			else if (typeof this.onunload == 'function')
			{
				this.onunload = function(e) {
					old_event();
					func();
				};
			}
			break;
		default:
	}
}

I use this feature in my previous post in tracking exit links in Google Analytics where I need to handle multiple onload events as my other Wordpress plugins uses the onload event as well.

An example of using this would be, window.addEvent(‘onload’, dtalk_ga.applyVirtualExits). Hope this helps.

[update date=01/09/08]
Thanks to ahot’s tip, I’ve shortened the code significantly now. Theoretically, this function should be able to work on all objects that support events.

/**
 *	@author:	Danny Ng (http://www.dannytalk.com)
 *	@date:		01/09/08
 *	@notes:		Free to use and distribute without altering this notice. Would appreciate a link back.
 */

/**
 *	@pre-condition: object must support events
 *	@usage: object.addEvent(event_type, function)
 *	@params: string, function
 */
function addEvent(e, func)
{
	var old_event = this[e];
	if (old_event)
	{
		this[e] = function(e) {
			old_event();
			func();
		};
	}
	else
		this[e] = func();
}

[/update]

Bookmark This!

http://www.dannytalk.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/newsvine_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/google_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/sphinn_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/twitter_48.png

Tracking Exit Links in Google Analytics

Posted by danny On August - 30 - 2008

I’ve been spending my time writing a script that will help track exit links (external links) in Google Analytics. This script will traverse through all anchor tags within the body and apply tracking codes on tags with the href attribute.

If the anchor tag has an onclick attribute already, it appends the tracking code to it and it shouldn’t affect the functionality.

I will try to post screenshots of how it looks in Google Analytics once I gather some data. To use it, simply paste the following code after the google analytics code and before the </body> tag.

<script type="text/javascript" src="<PATH-TO-SCRIPT>"></script>
<script type="text/javascript">
	dtalk_ga.applyVirtualExits();
</script>

You can view the source code here,

/**
 *	@author:	Danny Ng (http://www.dannytalk.com)
 *	@date:		30/08/08
 *	@updated:	26/09/08
 *	@notes:		Free to use and distribute without altering this notice. Would appreciate a link back.
 *	@usage:		Place the following code before the </body> tag and after google analytics code,
 *
 *				<script type="text/javascript" src="<PATH-TO-SCRIPT>"></script>
 *				<script type="text/javascript">
 *					dtalk_ga.applyVirtualExits();
 *				</script>
 */

var dtalk_ga = {
	applyVirtualExits: function() {
		var trackPage = function(a)
		{
			var domain_name = document.location.toString().toLowerCase().split('/')[2];
			var exit_domain = a.href.split('/')[2].toLowerCase();
			if (domain_name.toLowerCase().indexOf(exit_domain) == -1)
			{
				if (typeof pageTracker != 'undefined')
					pageTracker._trackPageview('/exit/' + a.href);
			}
		};
		var anchors = document.getElementsByTagName('a');
		for (var i = 0; i < anchors.length; i++)
		{
			if (anchors[i].href && anchors[i].href != (document.location + '#'))
			{
				if (typeof anchors[i].onclick == 'undefined')
					anchors[i].onclick = function(e) {
						trackPage(this);
					};
				else if (typeof anchors[i].onclick == 'function')
				{
					var old_onclick = anchors[i].onclick;
					anchors[i].onclick = function(e) {
						old_onclick();
						trackPage(this);
					};
				}
			}
		}
	}
};

Hopefully I can find more ways of writing scripts that will help with Google Analytics.

Bookmark This!

http://www.dannytalk.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/newsvine_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/google_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/sphinn_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/twitter_48.png

Quad in the office

Posted by danny On August - 20 - 2008

I’m so lucky. My company bought a couple of Quad core computers with 2gb ram for the new starters coming in. One is starting in a couple of weeks and another is coming back from holidays in a week.

So I took the opportunity to pinch the new systems and give them my old Pentium D.

It’s never felt so fast with Dreamweaver, Fireworks, Outlook, Firefox, Excel and Word open all at the same time! What an adrenalin rush :D Makes working in the office enjoyable.

Bookmark This!

http://www.dannytalk.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/newsvine_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/google_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/sphinn_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/twitter_48.png
Categories: Random

Travel Plans End of Year

Posted by danny On August - 19 - 2008

The end of the year is just around the corner and I’m excited to take a month off and have a little holiday. Thinking of heading off around the end of November to mid December period.

Prospects so far? 1-1.5 week in KL, 1.5-2 weeks in Sabah, and 1 week in Bali/Thailand/Vietnam/Cambodia, depending where my other mates want to go.

I’m hoping that the Australian dollar will spring back up again and make me a rich dude once I go to south-eastern countries.

Although I’m really keen on visiting a few places within the next few years: China, Africa and Alaska. Why China? Well coming from a Chinese heritage, I’ve always been fascinated by ancient Chinese history and culture. It really intrigues me on how rich Chinese history and culture is, providing many insights from early civilisation and wars to modern day revolution of China. There’s just so many things to see and discover!

Why Africa? I’d like to see and experience the third world countries in a way that will open up my eyes from my comfort of transportation, education, health, democracy and all other basic things in life that I take for granted. It would definitely put our (including myself) day to day complaints to shame and to realise how pathetic they are compared to the rest of the world whom 90% are probably living below poverty line.

Oh yes, I’d like to get some safari action too! I’m a big national geographic fan :)

Why Alaska? I don’t know. The fact that a 2-3 week cruise around the Alaskan glaciers and enjoying the beautiful untarnished environment of Alaska would probably be a big factor. Also the fact that I’ve never seen nor experienced snow in real life as well.

So many things to do while you’re still young. Better make the most of it ay?

Bookmark This!

http://www.dannytalk.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/newsvine_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/google_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/sphinn_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/twitter_48.png
Categories: Travel

Are we paying too much for public transport?

Posted by danny On August - 14 - 2008

I’ve been thinking recently amount costs relating to transport.

It will cost me $6 for a return bus ticket and $6.80 for a return train ticket. Amounts to $12.80 a day.

If I buy a travel 10 bus ticket and a city weekly train ticket, it will cost me $10.40 a day (assuming a Mon-Fri week).

If I buy a green weekly travel pass, it will cost me $8.60 a day (again, assuming a Mon-Fri week).

If I decide to drive to the city, it is approximately 20km from my house and assuming I’m driving a car that has an efficiency of 10L/100km, it will cost me $5.60 a day. This is with the assumption that petrol is $1.40/L and I travel 40km (to and fro).

To me, it seems that driving to work is a winner (if you exclude parking costs). So how come people are flocking in on the public transport system and trains are getting more crammed on a continuous basis? Everyday I feel like I’m in a can of sardines.

I think the media and Cityrail perhaps have provided a misconception that travelling by public transport is more cost effective. And let me tell you this, public transport fares ain’t going down with the demand of oil decreasing, pushing down the oil prices to around US$113 a barrel.

Bookmark This!

http://www.dannytalk.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/newsvine_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/google_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/sphinn_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/twitter_48.png
Categories: News
Tags: