Introduction into browser events

Ilya Kantor

  1. Assigning event handlers

    1. Using a attribute of HTML-tag

      1. When to use this method
    2. The element is this
    3. Using a DOM-object property
      1. When to use
    4. Special methods
      1. Microsoft solution
      2. Handlers assignment by W3C standard
    5. Handlers order
    6. A cross-browser way of assigning event handlers
  2. Summary

Most JavaScript-applications perform actions as a response to events.

An event is a signal from the browser that something has happened.

There are many types of events.

  • DOM events, which are initiated by DOM-elements. For instance, a click event happens when an element is clicked, a mouseover - when a mouse pointer comes over an element,
  • Window events. For instance, resize - when a browser window is resized,
  • Other events, like loadreadystatechange. They are used in AJAX and for other needs.

DOM events connect JavaScript code with the document, providing the means for building dynamical interfaces.

Assigning event handlers

For a script to react on the event, there should be a function assigned to it.

Functions which react on events are called event handlers. They are usually named like "on+event type", for instance: onclick.

JavaScript event handling is single-threaded, so handlers are executed sequentially. That means, if two events happen simultanteously, for example mouseover (mouse has come over an element) and mousemove (mouse moved over an element), their handlers will be executed one after another.

There are several ways of assigning an event handler. All of them are given in details below.

Using a attribute of HTML-tag

A handler can be set directly in the markup, right into the attribute named onevent.

For example, to process a click event on the input button, it is possible to assign an onclick handler like this:

<input id="b1" value="Click me" onclick="alert(‘Thanks!‘);" type="button"/>

In action:

The last example uses single quotes inside double quotes. An often newbie mistake is to forget that the code is inside an attribute.

Using them like onclick="alert("Click")" won’t work. If you really need it, tryonclick="alert(&quot;Click&quot;)". But usually you don’t. Read on for more event handling methods.

It is also possible to call a function for the event handling.
The example below runs a function count_rabbits() if a button is clicked.

01 <!DOCTYPE HTML>
02 <html>
03   <head>
04     <script type="text/javascript">
05       function count_rabbits() {
06         for(var i=1; i<=3; i++) {
07           alert("Rabbit "+i+" out of the hat!")
08         }
09       }
10     </script>
11  </head>
12  <body>
13     <input type="button" onclick="count_rabbits()" value="Count rabbits!"/>
14  </body>
15 </html>

Please recall that HTML-tag attribute names are case-insensitive, so oNcLiCk will work same as onClick oronclick.

But it is generally considered a good style to use lowercase.

When to use this method

This way of assigning handlers is very convenient - it’s simple and all-inline, that’s why it is sometimes used for really simple tasks.

There are certain drawbacks of this method. When a handler becomes longer than one line - readability suffers greatly.

But, after all, no one writes somewhat complex handlers in HTML. Instead of it, use JavaScript-only ways which are described in the next subsection.

  • A simple way for simple tasks
  • Mixed JavaScript-code and HTML-markup
  • Difficult to write complex handlers

The element is this

Although usage of this event-binding method is not recommended, let’s demonstrate the value of this with it.

Inside an event handler, this references the current element. It can be used to get properties on modify the element.

Below, the button outputs it’s contents using this.innerHTML:

<button onclick="alert(this.innerHTML)">Click me to see me</button>

Using a DOM-object property

A closest relative of the way described above - is an assignment using the property named onevent.

All you need is:

  1. To get an element
  2. To assign a handler to the property onevent

Here is an example of setting a click handler to the element with id="myElement":

1 <input id="myElement" type="button" value="Press me"/>
2 <script>
3 document.getElementById(‘myElement‘).onclick = function() {
4     alert(‘Thanks‘)
5 }
6 </script>

In action:

Please, note the two details:

  1. It is a property, not an attribute. The name of the property is onevent, case-sensitive and must belowercasedonClick won’t work.
  2. The handler must be a function, not a string.

When the browser meets an on... attribute in HTML-markup - it basically creates a function from its contents and assigns it to the property.

So these two codes do the same:

  1. Only HTML:

    1 <input type="button" onclick="alert(‘Click!‘)" value="Button"/>
  2. HTML + JS:
    1 <input type="button" id="button" value="Button"/>
    2 <script>
    3  document.getElementById(‘button‘).onclick = function() {
    4      alert(‘Click!‘)
    5  }
    6 </script>

If there is a handler set in markup, the script overwrites it. In the example below, JavaScript replaces a markup handler with a new one.

1 <input type="button" onclick="alert(‘Before‘)" value="Press me"/>
2 <script>
3 document.getElementsByTagName(‘input‘)[0].onclick = function() {
4   alert(‘After‘)
5 }
6 </script>

Of course, it is possible to use an existing function:

1 function doSomething() {
2   alert(‘Thanks!‘)
3 }
4  
5 document.getElementById(‘button‘).onclick = doSomething

An often newbie mistake

Please, note that the function should be assigned, namely doSomething, notdoSomething():

document.getElementById(‘button‘).onclick = doSomething

doSomething() - is a result of function execution, and because there is no return in it, the result will be undefined.

Compare it against an attribute. Brackets are required there:

<input type="button" id="button" onclick="doSomething()"/>

The difference is easy to explain. When the browser comes across onclick attribute, it automatically creates a function from its contents. So the last example is basically same as:

document.getElementById(‘button‘).onclick = function() {
  doSomething()  // an autocreated function
}

When to use

Assiging handlers using a property is a very simple and popular way.

It has a problem: only one handler for a certain event type can be set.

For example:

input.onclick = function() { alert(1) }
// ...
input.onclick = function() { alert(2) } // replaces the previous handler
  1. A convenient and reliable way, works in JavaScript
  2. A single handler per event

Of course, it’s possible to copy old handler and run it manually inside a new one. But it is better to use more advanced methods of assignment.

Special methods

In a complex JavaScript application, it’s fairly ok that different interface components may be interested in handling the same event.

A classical example is a “document loaded” event and many graphical components which wait for it to initialize themselves.

Microsoft solution

The solution provided by Microsoft and used only in Internet Explorer less than 9.

It is also supported by Opera for compatibility, but no one uses it there, because Opera also supports another standard-compliant method (see in the next section).

Assigning a handler:

element.attachEvent( "on"+event, handler)

Removing a handler:

element.detachEvent( "on"+event, handler)

For instance:

1 var input = document.getElementById(‘button‘)
2 function handler() {
3     alert(‘Thanks!‘)
4 }
5 input.attachEvent( "onclick" , handler) // assign the handler
6 // ....
7 input.detachEvent( "onclick", handler) // remove the handler

An often newbie mistake

Please, note - setting and removal methods need the same handler object to operate correctly.

This would be wrong:

1 input.attachEvent( "onclick" ,
2    function() {alert(‘Thanks‘)}
3 )
4 // ....
5 input.detachEvent( "onclick",
6    function() {alert(‘Thanks‘)}
7 )

In the example below, there are actually two different function objects.

So if it is planned to remove the handler sometime, the reference to it should be stored somewhere.

Using attachEvent, it is possible to assign multiple handlers to the same event on same element. The example below will work only in IE and Opera:

01 <input id="myElement" type="button" value="Press me"/>
02  
03 <script>
04   var myElement = document.getElementById("myElement")
05   var handler = function() {
06     alert(‘Thanks!‘)
07   }
08   
09   var handler2 = function() {
10     alert(‘Thanks again!‘)
11   }
12  
13   myElement.attachEvent("onclick", handler)
14   myElement.attachEvent("onclick", handler2)
15 </script>

attachEvent does not pass `this`

The exception is attachEvent method. Handlers assigned with attachEvent do not havethis!

Handlers assignment by W3C standard

W3C or official event handler assignment works in all modern browsers and for IE9.

Assigning a handler:

element.addEventListener( event, handler, phase)

Removing a handler:

element.removeEventListener( event, handler, phase)

Please, note that the event name goes without the “on” prefix.

Another difference from the Microsoft syntax is the third parameter - phase, which is usually not used and set to false.

The usage is generally same as attachEvent:

1 // ... declare a function called handler ...
2 elem.addEventListener( "click" , handler, false// assign the handler
3 // ....
4 elem.removeEventListener( "click", handler, false// remove the handler

So, there is a one big plus and one minus of the special methods:

  1. As many handlers as you want
  2. Cross-browser incompatibilities

The incompatibilities is not just different syntax, but there are few other differences. We’ll return to it in the next sections and discuss a cross-browser method of event handling.

Handlers order

Special methods allow to assign multiple handlers to the same event on single object.

Browser does not guarantee the order in which they execute.

Generally, the order of assignment is not related with the order of execution. The order may happen to be same, or inversed or random.

A cross-browser way of assigning event handlers

The task is not so simple as it seems.

There simplest and mostly working solution is to create custom functions which add and remove event handlers using special methods:

01 if (document.addEventListener) {
02     var addEvent = function(elem, type, handler) {
03         elem.addEventListener(type, handler, false)
04     }
05     var removeEvent = function(elem, type, handler) {
06         elem.removeEventListener(type, handler, false)
07     }
08 else {
09     var addEvent = function(elem, type, handler) {
10         elem.attachEvent("on" + type, handler)
11     }
12     var removeEvent = function(elem, type, handler) {
13         elem.detachEvent("on" + type, handler)
14     }
15 }
16  
17 ...
18 addEvent(elem, "click"function() { alert(‘hi‘) })

It works good in most cases, but the handler will lack this in IE, because attachEvent doesn’t providethis.

Fixing this problem may look easy, but it actually isn’t, because of advanced topics like IE<8 memory leaks.

But you don’t need this and don’t care about memory leaks, then the solution is simple and works well.

Use JavaScript to make the button hide the element with id="hide" when clicked. Demo:

The source document is here.

Solution

The solution is demonstrated here: tutorial/browser/events/task/hideOther.html.

Create an input button which hides itself when clicked.

Like this:

Open solution

Create a menu which opens/closes on click, like this:

The source and images to start from are at tutorial/browser/events/sliding-src/index.html.

Open hint 1Open solution

There is a message list. Add a delete button to each message to remove it.

The result:

The source is here.

Hint 1

Hint 2

Hint 3

Solution

The solution is shown here.

Summary

There are 3 ways of assigning event handlers: markup, onevent and special methods.

时间: 2024-11-04 12:49:11

Introduction into browser events的相关文章

HTML5 Server-Sent Events With Java Servlets Exampl

Since the dawn of modern web applications, push notifications have gained significant traction in industry. Instead of pooling data from server it has now become common that server should notify client. Ajax is so pervasive these days it is almost se

Angular1 Attack!!!

1.构建一个CRUD(create retrieve update delete)应用需要用到什么? 数据绑定 基本模板标识符 表单验证 路由 深度链接 组件重用 依赖注入 2.运用AngularJs的(Services)和指令(directives)构建完整,健壮的应用. - 模板系统. It uses HTML as the templating language • It doesn't require an explicit DOM refresh, as AngularJS is ca

最详细的JavaScript和事件解读

与浏览器进行交互的时候浏览器就会触发各种事件.比如当我们打开某一个网页的时候,浏览器加载完成了这个网页,就会触发一个 load 事件:当我们点击页面中的某一个“地方”,浏览器就会在那个“地方”触发一个 click 事件. 这样,我们就可以编写 JavaScript,通过监听某一个事件,来实现某些功能扩展.例如监听 load 事件,显示欢迎信息,那么当浏览器加载完一个网页之后,就会显示欢迎信息. 下面就来介绍一下事件. 基础事件操作 监听事件 浏览器会根据某些操作触发对应事件,如果我们需要针对某种

XEVENT入门

XEVENT:在指定事件发生时记录,可将日志放在内存或文件中,效率比sqlprofile高.参考:Introduction to Extended EventsTroubleshooting Error 511 using XEvents msdn An XEvent A Day 相关DMV1相关DMV2 相关DMV: Events -- Event objects SELECT p.name AS package_name, o.name AS event_name, o.descriptio

WAITEVENT: &quot;library cache: mutex X&quot; (文档 ID 727400.1)

WAITEVENT: "library cache: mutex X" (文档 ID 727400.1) 2014-01-19 09:56 2367人阅读 评论(0) 收藏 举报  分类: 网络资源_ORACLE_调优(108)  目录(?)[-] APPLIES TO PURPOSE SCOPE DETAILS Definition Individual Waits Systemwide Waits Wait Time Reducing Waits Wait times Known

Document Object Model (DOM) Level 3 Events Specification

Document Object Model (DOM) Level 3 Events Specification W3C Working Draft 25 September 2014 This version: http://www.w3.org/TR/2014/WD-DOM-Level-3-Events-20140925/ Latest published version: http://www.w3.org/TR/DOM-Level-3-Events/ Latest editor's dr

js事件——Events

没有共产党就没有新中国,没有 events 就没有 scripts.每个包含 JavaScript 的页面中: 几乎所有的脚本都是由 event 触发的.原因很简单. JavaScript 可以增加交互性: user 做一些操作,页面给出响应.Events 是所有 JavaScript 应用的小心脏.本文介绍了事件处理机制,相关问题以及如何写出跨浏览器的 scripts.同时也展示了几个带有事件处理细节的页面. 因此JavaScript 检测用户的操作,以便知道何时做出反应,反应时执行哪个 fu

High Performance Browser Networking

Table of Contents Foreword Preface About This Book Conventions Used in This Book Safari? Books Online How to Contact Us Content Updates May 23, 2014 I. Networking 101 1. Primer on Latency and Bandwidth Speed Is a Feature The Many Components of Latenc

[转] Introduction to AppArmor

Introduction to AppArmor http://ubuntuforums.org/showthread.php?t=1008906 Contents Post 1 Introduction (This is it). Post 2 AppArmor on Ubuntu. Post 3 Anatomy of a Profile. Post 4 Generating Profiles. Introduction The intent of this post is to increa