search
HomeWeb Front-endJS TutorialJavaScript Event Learning Chapter 3 Early Event Handlers_Javascript Skills

These ancient browsers only support one method of registering event handlers, which was invented by Netscape. Because Netscape struck first, if Microsoft wanted to build a browser that supports JavaScript events, it would have to follow Netscape's lead, so there is no compatibility issue here. So this mode will work in any browser that supports JavaScript --- except IE3 on Mac, which does not support events at all.
Register event handler
In the inline event registration model, the event handler is like an attribute of an HTML element, such as:

When a click event occurs on this link, the event handler is triggered and your script is executed: an alert dialog box pops up. You can also trigger a JavaScript function:

The case of the event names in the above two examples is just a matter of habit, HTML It is not case sensitive, so you can write it however you want. XHTML requires that all attribute names must be lowercase, so if you are using XHTML then the name must be written as onclick.
Don’t use it
Although this inline registration model is very old and reliable, it has a shortcoming. He requires you to write JavaScript code inside the XHTML structure layer that does not belong here.
So I strongly recommend you not to use this method. I have a detailed explanation here.
Understanding these old models helps a lot in getting a global feel for JavaScript event handling, but you're better off using the modern models I'll explain below.
Default Action
Back then, Netscape set a default action and there was a way to prevent the default action from running. His model saved the browser wars and standards, and it still works well today.
As we all know, when a user clicks on a link, the browser will load the page according to the href attribute. This is the default action on links. But what happens when you define an onclick event handler? It should be implemented, but when?

If you click on this link, the event handler will be executed first. After all, when the default action occurs - a new page is loaded - the old page including the event handler itself is cleared from memory. If the onclick event handler is executed, it must be before the default action.
This has a very important principle. If an event triggers both the default action and the event handler, then:
, the event handler will be executed first
, and the default action will be executed later
. So in the above example, doSomething() will be executed first, The browser will then open the link.
Blocking default events
After these are determined, most people start to think about how to prevent default events. In our example we can prevent the browser from opening new pages.
So the event handler can return a Boolean value (true or false). The meaning of false is "do not perform the default action". In this way, we can change the example to:

This link will not be executed. After this function is executed, the program returns false, telling the browser not to perform the default action.
Sometimes it is necessary to let the function decide when it should perform and when it should not perform the default action. So we can change the example to:

Copy the code The code is as follows:

This is (very simple) user interaction. The user will be asked a question, and if the answer is yes then the function returns true, if it has been canceled for so long it returns false. This return value is captured by the event handler and passed to the event itself. If it is flase then the default action will not be executed - the link will not be entered.
However, not all default actions can be blocked. For example, the unload event does not work. Assume the user closes the browser window - the unload event is triggered. If you can prevent the window from closing, will the window remain open against the user's wishes? Of course not.
You can try Microsoft's beforeunload attribute to prevent unloading. Instead of creating a very confusing situation for the user to choose to confirm this action. It's better not to use it.
Returning false to prevent the default action is supported by all browsers. This is the basic component of an event handler. Today's event handler model also adds some new methods to prevent default actions:
W3C added the preventDefalut() method to events. If you reference this method then the default action will be blocked.
Microsoft added the returnValue attribute to the event. If you set its value to false then the default action will also be blocked.
But there is no need for these, simply returning false is enough.
window.status
There is an exception here for returning false. After you set the status bar of the window to change when the mouse passes over a link, and you want to prevent the default action of displaying the link address in the status bar, you should return true:
Copy code The code is as follows:

If you don't do this, the code won't work. No one knows what's going on, it's just a weird thing.
this
In JavaScript, the this keyword usually refers to the owner of the function. If this points to the HTML element where the event occurred, then everything is fine and you can do a lot of things easily.
Unfortunately, although this is very powerful, it is still difficult to use if you don't know exactly how it works. I've discussed this in detail in another place, but here I'll give an overview in inline mode.
In inline mode you can pass this as a parameter to an event handler function. So you can:
Copy the code The code is as follows:

You give The function is passed a reference, which is stored in obj. Now you don't need to traverse the document to find which element was clicked: it's safely stored in the variable obj. Now you can:
Copy the code The code is as follows:

The function accepts a link The reference is stored in obj. Now you can read the link address of this link and confirm it. You can apply this trick to any link: it will always show the real address of the link you just clicked on.
Continue
If you need to continue learning, please read the next chapter.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment