Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- Player embed code with an id attribute -->
  <video-js id="myPlayerID"
  style="width: 640px; height: 360px;"
  data-video-id="4845798282001"
  data-account="1752604059001"
  data-player="default"
  data-embed="default"
            class="video-js"
  controls></video-js>
<script src="https://players.brightcove.net/1752604059001/default_default/index.min.js"></script>

<!-- Display the number of video views the user has watched -->
<p>Number of views = <span id="cookieDisplay1"></span></p>
              
            
!

CSS

              
                /* * The body style is just for the
 * background color of the codepen.
 * Do not include in your code.
 */
body {
  background-color: #111;
  color: #fff;
}
/*
 * Styles essential to the sample
 * are below
 */
              
            
!

JS

              
                videojs.getPlayer('myPlayerID').ready(function() {
    // Function to read the browser cookie
    var read_cookie = function (key) {
        var result;
        return (result = new RegExp('(^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? result[2] : null;
    }

    // +++ Read browser cookie +++
    // Initialize variables and read the browser cookie associated with Brightcove video views
    var myPlayer = this,
        videoStart = 0,
        currentPosition,
        cookie = read_cookie("BC_views");
    //console.log("BC_views cookie: ",cookie);

    // +++ Define custom error message +++
    // Define an error message for when the user has reached the max number of views
    myPlayer.errors({
        "errors": {
            "-3": {
                "headline": "You've reached your maximum views.",
                "type": "USER_MAX_VIEWS",
                "message": "Please come back tomorrow."
            }
        }
    });

    // Display the cookie value
    // Comment out the next line when using the iframe implementation
    document.getElementById("cookieDisplay1").innerHTML = cookie;
  // +++ Display custom error message +++
    // If the cookie value is greater than or equal to 2, then display the custom error message
    if (cookie != null && cookie >= 2) {
        // Wait for the player to be loaded before displaying the message
        myPlayer.on("loadedmetadata", function () {
          // The dismiss:false property hides the close button when the message is displayed. This keeps the user from closing the error message and playing the video.
            myPlayer.error({
              code: '-3',
              dismiss: false
            });
        })
    }

    // +++ Increment the cookie +++
    // Each time the user plays the video, increment the browser cookie
    myPlayer.one("play", function () {
        var d = new Date();
        d.setTime(d.getTime() + (1 * 24 * 60 * 60 * 1000));
        cookie++;
        document.cookie = "BC_views=" + cookie + ";expires=" + d.toUTCString();
    });
});
              
            
!
999px

Console