• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Basics / The onabort Event in JavaScript

The onabort Event in JavaScript

The onabort Event in JavaScript

The JavaScript onabort event is triggered when a user aborts an action, such as when they stop loading an image or cancel a form submission. This event can be used to handle such scenarios and provide feedback to the user.

To use the onabort event, you can add the event listener to the relevant HTML element, such as an <img> tag for handling image loading. Here is an example of how to handle an image load abort:

<img id="myImage" src="example.jpg" onabort="imageAborted()">

<script>
    function imageAborted() {
        alert("Image load was aborted by the user.");
    }
</script>

In the example above, the onabort attribute is added to the <img> tag, and the imageAborted() function is called when the event is triggered.

You can also use the addEventListener() method to attach the onabort event to an element. Here is an example of using this method to handle a form submission abort:

<form id="myForm">
    <input type="text" placeholder="Enter your name">
    <input type="submit" value="Submit">
</form>

<script>
    var form = document.getElementById("myForm");
    form.addEventListener("abort", formAborted);

    function formAborted() {
        alert("Form submission was aborted by the user.");
    }
</script>

In the example above, the addEventListener() method is used to attach the formAborted() function to the onabort event for the <form> element.

It’s worth noting that the onabort event only works on certain types of elements and actions, such as image loading and form submissions. It is not supported on all elements and you can use it only on specific element like <audio>, <embed>, <img>, <object>, <video>.

Basics

Related Snippets:

  • Javascript Conditionals Explained
  • What JavaScript Closures Are and How They Work
  • JavaScript Objects Explained
  • The onblur Event in JavaScript

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us