- JavaScript:Moving to ES2015
- Ved Antani Simon Timms Narayan Prusty
- 397字
- 2021-07-09 19:07:42
Working with browser events
When are you developing for browsers, you will have to deal with user interactions and events associated to them, for example, text typed in the textbox, scrolling of the page, mouse button press, and others. When the user does something on the page, an event takes place. Some events are not triggered by user interaction, for example, load
event does not require a user input.
When you are dealing with mouse or keyboard events in the browser, you can't predict when and in which order these events will occur. You will have to constantly look for a key press or mouse move to happen. It's like running an endless background loop listening to some key or mouse event to happen. In traditional programming, this was known as polling. There were many variations of these where the waiting thread used to be optimized using queues; however, polling is still not a great idea in general.
Browsers provide a much better alternative to polling. Browsers provide you with programmatic means to react when an event occurs. These hooks are generally called listeners. You can register a listener that reacts to a particular event and executes an associated callback function when the event is triggered. Consider this example:
<script> addEventListener("click", function() { ... }); </script>
The addEventListener
function registers its second argument as a callback function. This callback is executed when the event specified in the first argument is triggered.
What we saw just now was a generic listener for the click
event. Similarly, every DOM element has its own addEventListener
method, which allows you to listen specifically on this element:
<button>Submit</button> <p>No handler here.</p> <script> var button = document.getElementById("#Bigbutton"); button.addEventListener("click", function() { console.log("Button clicked."); }); </script>
In this example, we are using the reference to a specific element—a button with a Bigbutton
ID—by calling getElementById()
. On the reference of the button element, we are calling addEventListener()
to assign a handler function for the click event. This is perfectly legitimate code that works fine in modern browsers such as Mozilla Firefox or Google Chrome. On Internet Explorer prior to IE9, however, this is not a valid code. This is because Microsoft implements its own custom attachEvent()
method as opposed to the W3C standard addEventListener()
prior to Internet Explorer 9. This is very unfortunate because you will have to write very bad hacks to handle browser-specific quirks.
- ASP.NET Web API:Build RESTful web applications and services on the .NET framework
- Modular Programming with Python
- 無代碼編程:用云表搭建企業(yè)數(shù)字化管理平臺(tái)
- Python 深度學(xué)習(xí)
- Machine Learning with R Cookbook(Second Edition)
- Learning SAP Analytics Cloud
- 云原生Spring實(shí)戰(zhàn)
- SQL Server 2016數(shù)據(jù)庫應(yīng)用與開發(fā)習(xí)題解答與上機(jī)指導(dǎo)
- OpenStack Orchestration
- Python Interviews
- jQuery技術(shù)內(nèi)幕:深入解析jQuery架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)原理
- Go語言從入門到精通
- Oracle 12c從入門到精通(視頻教學(xué)超值版)
- 多媒體技術(shù)及應(yīng)用
- 軟件測(cè)試技術(shù)