Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7740311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:44:43+00:00 2026-06-01T08:44:43+00:00

I have a video element and a script that attaches an onplay handler to

  • 0

I have a video element and a script that attaches an onplay handler to the video via jQuery:

<video id="vid" />
<!-- ... -->
<script type="text/javascript">
    $('#vid').on('play', function () {
        $(this).addClass('playing');
    });
</script>

According to this answer, users might be able to interact with the video player before the script is loaded. But I need to make sure that my event handler is attached before the onplay event can be fired.

To test this, I added a script right after the video that calls play(). The handler is run on all major browsers except WebKit, which doesn’t play the video at all. Even wrapping the jQuery statement in a $().ready() function still lets the handler be attached before onplay.

Is it guaranteed that my script will never miss any events from the video in HTML5? Does the same apply to all DOM events? What if I add async to the script?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:44:45+00:00Added an answer on June 1, 2026 at 8:44 am

    A race condition in that case used to be possible:

    <video autoplay src="…"></video>
    <script>alert("wait!")</script>
    <script>$('video').on('play',…)</script>
    

    However, the spec has been changed to alleviate this problem. Now browsers are supposed to queue firing of events until next event loop run, instead of firing them immediately.

    If the script attaching event handlers is an inline script immediately following the <video>, running without waiting for DOMReady, then race condition won’t happen in compliant browsers.

    If you use external or async script, wait for DOMReady, use setTimeout or synchronous XHR, then you may miss those events, since those things can cause event loop to be processed (although I’m not 100% sure whether network stall allows browsers to process the event loop).


    I don’t know whether WebKit has been updated to support the safer behavior. If you still have problems, then you can use methods that work even when events are fired instantly:

    1. Use inline event handler:

      <video onplay="…">
      
    2. Create the element programmatically (document.createElement('video')) with handlers attached before it’s inserted into the document.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a <video> element that is generated by js, and I need to
I'm trying to play a video and have a button that switches the video
I have a an HTML5 video element that plays and communicates with a some
I have a video element set to 100% width in a container div. That
I have a video element on a page which has an mp4, ogg, and
I have video in my project. I play it with MPMoviePlayerController but app will
I have video served by Ooyala that plays fine on all devices. The problem
I have one video object and 3 thumbnails which, when clicked, will play different
I have large video files (~100GB) that are local on my machine. I have
I have a piece of jQuery that requests a file (using .load() method) with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.