Tag Archives: javascript

Javascript: Event.target and Event.srcElement

When working with events and making your code compatible with other browsers it’s important to remember to use the right target property depending what browser you are in.

For example if you are using Firefox then you can simple do this:

This will output an element which trigger the event. Now if you want to achieve the same thing in Internet Explorer (versions 6-8) you have to use different approach because event listeners are attached using a non-standard element.attachEvent() method.

To make things shorter and better, I recommend using a short notation to target both situations at the same time depending on the browser the client is using, like this:

You can read up more about this here.

Javascript – how to stop/terminate execution

When debugging I often find myself printing many values to the screen and ending script execution (in PHP) to verify that I got the right values. Well, since JavaScript loads last and you will get most of your page loaded, you can use the combination of console.log() function and throw statement.

The throw statement will intentionally throw an error or a specified statement, but most of all it will terminate your JavaScript code.

Quick and easy.