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

  • SEARCH
  • Home
  • 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 7398547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:53:00+00:00 2026-05-29T03:53:00+00:00

How can I insert ads on html5 video tag before the main video plays?

  • 0

How can I insert ads on html5 video tag before the main video plays? Is there any open source tools to make this easier? Is there any reference that can guide me there?

It is working with this code:

<script type="text/javascript">
   // listener function changes src
   function myNewSrc() {
      var myVideo = document.getElementsByTagName('video')[0];
      myVideo.src="../main.webm";
      myVideo.load();
      myVideo.play();
    }

   // function adds listener function to ended event -->

    function myAddListener(){
     var myVideo = document.getElementsByTagName('video')[0];
     myVideo.addEventListener('ended',myNewSrc,false);
    }
</script>

but I can’t when it play the second one. It shows the poster. How do I get rid of the poster?

  • 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-05-29T03:53:00+00:00Added an answer on May 29, 2026 at 3:53 am

    This is a quick off the cuff start of a solution that should at least point you in the right direction. This gives you a singleton with an init method that when called sets up a preroll on a particular video element.

    var adManager = function () {
        var vid = document.getElementById("myVid"),
            adSrc = "videos/epic_rap_battles_of_history_16_adolf_hitler_vs_darth_vader_2_1280x720.mp4",
            src;
    
        var adEnded = function () {
            vid.removeEventListener("ended", adEnded, false);
            vid.src = src;
            vid.load();
            vid.play();
        };
    
        return {
            init: function () {
                src = vid.src;
                vid.src = adSrc;
                vid.load();
                vid.addEventListener("ended", adEnded, false);
            }
        };
    }();
    

    There are a number of things that aren’t covered here, though. For instance, if you set the init method to be called when you start playing the video, you’ll need to keep a flag that indicates whether there’s an ad playing so that the play handler won’t do anything when you’re transitioning from the ad to the content (which requires a “play” event after the load() call in order to get the seamless playback).

    We use something similar in our video playing project, and most of the video ad services out there do something like this for HTML based video playback (as opposed to Flash video playback).

    It’s relatively straightforward, but you just have to make sure to keep track of when event callbacks should be fired and when to add and remove those callbacks.

    Another thing to consider is the unreliability of the “ended” event. I haven’t yet figured out when and on which platforms it consistently fires, but it’s a fairly well known problem. A possible solution is to use “timeupdate” instead and test whether the “currentTime” property is somewhere around a second less than the “duration” property so you know you’re right at the end of the video.

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

Sidebar

Related Questions

Is there any way I can insert an HTML template to existing DOMNode without
I know that you can insert multiple rows at once, is there a way
Is there a way that I can insert values into a VB.NET Dictionary when
I now that I can insert text to <div> tag by : <script type=text/javascript>
Is there a way I can format a list so I can insert it
There was a previous question on StackOverflow about this subject (can insert the link,
Does anyone know if there is a way I can insert values into a
Is there a space or other non-printing character I can insert to a varchar2
how do I make a text box which the user can insert text into,
Is there a way that I can insert content at the beginning of a

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.