Read, With the Name of Your Lord Who Created

Posts Tagged ‘Events’

Four Ways Javascript Binding Event Listeners

Posted by triaslama on July 22, 2008

There is several ways in how to bind an event in Javascript. As far as I know four ways exist on behalf of binding a Javascript event. Here brief description of each way

1. Through inline HTML Code
The Javascript event declared inline in HTML code, for example:

<input type=”button” value=”Alert Message” onclick=”showAlert()” />

Then showAlert() is a Javascript function that merely show an alert message:

function showAlert()
{
      window.alert("Hello, World!!!");
}

This inline event binding works for all browsers.

2. Traditional Binding
I think this way is more elegance than the first one, but before we can do this we must get the desired element. We can use two useful methods of document object to get the element we want, there is getElementById() and getElementsByTagName(). Both method receive one string parameter which show the ID of an element (for getElementById()) or elements name (for getElementsByTagName()). Please aware that the result of getElementsByTagName() is always array of element.
Suppose that we have the following page:

Read the rest of this entry »

Posted in Javascript, Programming | Tagged: , , , , , | 18 Comments »

Javascript Event Phases: Capturing and Bubbling

Posted by triaslama on July 3, 2008

Events is something useful in programming world, because with events program can react based on user action. Javascript is no exception and I think one of Javascript strength is Events. There is many interesting things in Javascript events one of them are the Javascript event phases: Capturing and Bubbling.

Two Phases of Javascript Event: Capturing and Bubbling
Capturing phase is an event moving down the DOM tree to the element that instantiated event. Bubbling phase occuring after Capturing phase. Bubbling phase begins from the source of the event (e.g. button click) traverses up DOM tree to the root element.

Let’s see the following document structure:

<body>
<form>
<input type="button" />
</form>
</body>

When we click the button, the event captured by the document first, then <body> element, <form> element, and finally the <input type=”button” /> element (capturing phase). After that the event moves back, started from button element (button click event handler fired), <form>’s click, <body>’s click, and document click event handler fired (bubbling phase).

For the complete example try the following (evtphases.htm):
Read the rest of this entry »

Posted in Javascript, Programming | Tagged: , , , , , | 2 Comments »

Javascript Events: Programmatically vs. Declaratively

Posted by triaslama on February 14, 2008

In the past few days I was enjoyed learning Javascript and I quite interested when I read about Javascript event! The Javascript event is a bit different from the events I knew before, and I guest it quite powerful too.
Look at the simple example:
when we declares an event programmatically we can do this (it works for IE and non IE browsers):

<html>
<head>
<title>Hello Javascript</title>
<script lang=”javascript” type=”text/javascript”>
document.onclick = function(e)
{
e = e||window.event;
window.alert(e.type);
};
</script>
</head>
<body>
</body>
</html>

We need do a reguler check with this line of code:

e = e||window.event;

because Internet Explorer (IE) recognize an event object throught the property of object window named ‘event’ so when we want to get the event object in IE programmatically it can be accomplished using ‘window.event’.

We can change the name of ‘e’ parameter above with the name we like (such ‘event’, ‘evt’, ‘myParameter’, etc.), and the code will still work fine.

but when I do this :
<html>
<head>
<title>Hello Javascript</title>
<script lang=”javascript” type=”text/javascript”>
function test(e)
{
window.alert(e.type);
}
</script>
</head>
<body>
<input type=”button” id=”btn” value=”Test” onclick=”test(e)” />

</body>
</html>

I type the path in browser’s address bar and click the button but the ‘e’ parameter is undefined! Then I wonder why, because in my opinion the event handler always receive one parameter (that is the event object itself).
As an experiment I change the code above so it looks like the following
<input type=”button” id=”btn” value=”Test” onclick=”test(event)” />
And now the code just work fine! What happen? is that Javascript event so strange like this?

So I do a litle experiment on how to declare event programmatically (through javascript code) and declaratively (through html tag), and then I found the answer.
When we declare events programmatically the event handler function can accept zero parameter (no parameter required), or the event handler function can accept one parameter (the event object itself).
When we declare events declaratively (specify the event handler inside a html tag, such as <input type=”button” onclick=”clickHandler()” />) we can pass any number of parameters, but when we want to pass the event object as parameter we must pass it as ‘event’ nothing else!

I think this is quite interesting story from Javascript event, and I hope I can find more interesting things. See you…

(Tri Sugiyantowo / triaslama).

Posted in Javascript | Tagged: , , , , | 2 Comments »

Javascript: Adding OnMouseOver And OnMouseOut Using DOM

Posted by triaslama on February 2, 2008

The DOM (Document Object Model) is a standard way to access a document within a browser. The DOM could be Core DOM, HTML DOM, XML DOM. The DOM can be very handy and useful when we need to access the specific elements of the document and add some effects to them.

The two of the most useful of the DOM methods could be getElementById and getElementsByTagName. The getElementById will return an element that specified as a parameter in getElementById DOM method. The parameter that passed is a string that represents the ID of an element. The getElementsByTagName -that receive one parameter, the element name as string- will return array that represents elements in the document with the tag name specified as a parameter in getElementsByTagName method.

Here a simple example utilization of getElementById and getElementsByTagName method to add an effect -change the background color- when the mouse cursor over a <li> element and reset the background color when then mouse cursor out a <li> element:

SimpleDom.htm

<html>
<head>
<title>Test Something</title>
<script lang=”javascript” type=”text/javascript”>
window.onload = function()
{
var lis = document.getElementsByTagName(“li”);

for (var i=0;i<lis.length;i++) {
lis[i].onmouseover = function()
{
this.style.backgroundColor = “gainsboro”;
};
lis[i].onmouseout = function()
{
this.style.backgroundColor = “white”;
};
}
};
</script>
</head>
<body>
<ul>
<li>First Element</li>
<li>Second Element</li>
<li>Third Element</li>
</ul>
</body>
</html>

Let’s look at the code in more details. The body of the document consist of an unordered list (ul). The <ul> element have three list item <li> element as childs. The scenario is simple change the background color of <li> element when the mouse is moved over around them and reset the background color of <li> element when the mouse is moved out of them.

To attain this functionalities we using Javascript and DOM to access the document specified in the <body> element. The first line of our Javascript code is window.onload, this function is executed when the window object is fully loaded, in the other word the window.onload executed only when the elements inside a document has been rendered.

To handle window.onload we create an inline function that tells what must be done when the page has been loaded. In this case we will retrieve all of <li> element in a document using getElementsByTagName DOM method and save this element in a variable named lis.

var lis = document.getElementsByTagName(“li”);.

The next step iterate through all of <li> element inside lis variable:

for (var i=0;i<lis.length;i++).

But the most important part here we install two events for each of <li> element, onmouseover (happens when the mouse cursor over an <li> element) and onmouseout (happens when the mouse cursor out from an <li> element).

To handle the onmouseover event we create an inline function that changes the background color of <li> element when the mouse cursor over it.

lis[i].onmouseover = function()
{
this.style.backgroundColor = “gainsboro”;
};

To handle event when the mouse cursor out of an <li> element we create an inline function that reset the background color back to its original color.

lis[i].onmouseout = function()
{
this.style.backgroundColor = “white”;
};

The word this above refers to the <li> element that being iterated. When everything is ready and we move the cursor over an <li> element we should see that the background color of <li> that is being hovered change to gainsboro, and the background color will back to white when the mouse cursor leaves the specified <li> element.

The Javascript and DOM can have more functionalities and usages I hope this simple article will give me and all the readers a clue about the advantage of DOM.

Posted in DOM, Javascript | Tagged: , , , , , | 27 Comments »