• This repository has been archived on 19/Apr/2019
  • Stars
    star
    302
  • Rank 138,030 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

jQuery.html5Loader can preload images, SVGs, html5 video and audio sources, scripts, css, and text files.

Introduction

jQuery.html5Loader can preload images, SVGs, html5 video and audio sources, css, scripts and text files. This plugin needs a JSON file to get the files that must be preloaded (you can use also use a javascript object as well), and it provides an easy API to give you the right amount of files loaded in percentage.

All the javascript and css files will be automatically loaded and injected into the DOM

Installation

$ bower install jquery.html5loader

# or via npm

$ npm install jquery.html5loader

Features

  • smart: it loads just the sources supported by the client running the script.
  • flexible: it returns the current percentage and the object/element loaded, so you could be free to show this info as you prefer
  • fun: inside the package you could find some preloading animation examples, customizable and ready to use

Demos

Production websites using jQuery.html5loader

USAGE

1 Create a JSON file like this, containing all the files you need to preload ( size in bytes ):

{
    "files": [
      {
        "type":"SCRIPT",
        "source":"../path/to/your/script.js",
        "size":4.096,
        "stopExecution":true
      },
      {
        "type":"SCRIPT",
        "source":"../path/to/your/script.js",
        "size":4.096,
        "stopExecution":false
      },
      {
        "type":"IMAGE",
        "source":"../path/to/your/image.jpg",
        "size":620
      },
      {
        "type": "CSS",
        "source": "../files/test.css",
        "size": 16.819
      },
      {
        "type":"TEXT",
        "source":"../path/to/your/text.txt",
        "size":44
      },
      {
        "source": {
          "svg": "../files/yin-yang.svg",
          "fallback": "../files/yin-yang.jpg"
        },
        "type": "IMAGE",
        "size": 2338
      },
      {
        "type":"VIDEO",
        "sources": {
          "webm":{
            "source":"../path/to/your/video.webm",
            "size":5054.976
          },
          "ogg":{
            "source":"../path/to/your/video.ogg",
            "size":2932.736
          },
          "h264":{
            "source":"../path/to/your/video.mp4",
            "size":9285.632
          },
          "vp9": {
              "source":"../path/to/your/video.webm",
              "size":9285.632
            }
          }
        }
      },
      {
        "type":"AUDIO",
        "sources": {
          "mp3":{
            "source":"../path/to/your/audio.mp3",
            "size":9285.632
          },
          "ogg":{
            "source":"../path/to/your/audio.ogg",
            "size":2089.688
          },
          "opus": {
            "source": "../path/to/your/audio.opus",
            "size":2000.20
          },
          "wav": {
            "source": "../path/to/your/audio.wav",
            "size":2000.20
          },
          "m4a": {
            "source": "../path/to/your/audio.m4a",
            "size":2000.20
          }
        }
      }
    ]
  }

2 Import the plugin into your page:

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="../js/jQuery.html5Loader.js"></script>

3 Initialize the plugin setting the callback functions:

$.html5Loader({
      filesToLoad:    '../js/files.json', // this could be a JSON or simply a javascript object
      onBeforeLoad:       function () {},
      onComplete:         function () {},
      onElementLoaded:    function ( obj, elm) { },
      onUpdate:           function ( percentage ) {}
});

API

Methods

  • stopExecution: default false, do not execute the javascript files when they'll be preloaded, this option could be overridden into the json per each javascript file
  • mediaBufferSizeToPreload: default 0.2, decide the the buffer size before considering the media preloaded
  • forceMediaPreload: default true, let the browser decide when the media file has been buffered enough to be played by listening the canplaythrough event
  • onBeforeLoad It is triggered right before the plugin starts loading all the files
  • onComplete It is triggered when the plugin finishes to load all the sources
  • onMediaError This function is called in case there's an error during the preloading
    • obj original object passed to the plugin
    • elm html output (for type "SCRIPT" and "TEXT" this value is just a string)
  • stopExecution (false by default) : default behavior for all the script files, they won't execute when loaded. This behavior can be changed by each script object passed to the plugin
  • onElementLoaded It is triggered anytime a new element of your files list gets loaded, (ATTENTION IF AN ELEMENT IS NOT SUPPORTED IT WILL NEVER PASS TROUGH THIS FUNCTION).
    • obj original object passed to the plugin
    • elm html output (for type "SCRIPT" and "TEXT" this value is just a string)
  • onUpdate it is triggered anytime new bytes are loaded
    • percentage the percentage currently loaded

Loading Segments

It is also possible to load groups of files in sequence by wrapping them in arrays. This could be handy if you need need to preload components with dependencies. e.g Backbone's dependence on underscore.

{
    "files": [
      [
        {
          "type":"SCRIPT",
          "source":"../path/to/underscore.js",
          "size":13.9
        }
      ],
      [
        {
        "source": "../path/to/backbone.js",
        "type": "SCRIPT",
        "size": 37.5
      },
      {
      "source": "../path/to/three.js",
        "type": "SCRIPT",
        "size": 150.3
      }
      ]
    ]
}

Loading SVGs

You can also load SVG files providing a fallback image. jQuery.html5Loader will detect automatically if the device supports the SVGs otherwise it will just preload the fallback image.

  "files":[
    {
      "source": {
        "svg": "../files/yin-yang.svg",
        "fallback": "../files/yin-yang.jpg"
      },
      "type": "IMAGE",
      "size": 2338
    }
  ]

KNOWN ISSUES

  • Internet Explorer 9 and 10 do not return any value using the method canPlayType on a video or audio element ( http://modernizr.com/docs/#audio ). For these browsers we don't preload any HTML5 media format
  • on mobile devices and on the iPad we cannot load any video or audio element because these devices can't preload those kind of elements until the user start dealing with them

TODO List

  • Write down a valid crossbrowser unit test

CHANGELOG

v1.8.0

  • added: SVGs preloading support
  • added: segments preloading
  • updated: the nprogress example

v1.7.0

  • added: better tablet detection tests
  • added: grunt
  • bugfix: skip media elements loading on mobile/tablet devices

v1.6.9

  • added: mediaBufferSizeToPreload option
  • added: forceMediaPreload option

v1.6.8

  • small refactor
  • added: new media file codecs tests
  • video: vp9
  • audio: opus, wav, m4a
  • added: stopExecution option
  • added: stopExecution option via json
  • added: nprogress demo

More Repositories

1

allora

Promisify using es6 Proxies every javascript API with less than 50 lines of code
JavaScript
679
star
2

Tocca.js

Super lightweight script (~1kb) to detect via Javascript events like 'tap' 'dbltap' 'swipeup' 'swipedown' 'swipeleft' 'swiperight' on any kind of device.
JavaScript
659
star
3

Vague.js

Vague.js is an experimental script that allows you to blur any kind of html element thanks to the SVG filters
JavaScript
604
star
4

icaro

Smart and efficient javascript object observer, ideal for batching DOM updates (~1kb)
JavaScript
572
star
5

es6-project-starter-kit

Universal Starter Kit to build any javascript ES6 project/library runnable in nodejs and on any browser.
JavaScript
460
star
6

jQuery.BlackAndWhite

Clientside grayscale images on any browser
JavaScript
311
star
7

parallax

ES6/ES2015 HW accelerated scrollable images parallax
JavaScript
276
star
8

go-observable

It allows you to send and receive events with a tiny simple API
Go
240
star
9

nodejs-MySQL-push-notifications-demo

Node server that is able to read from a MySql database and then stream those data via websocket to many client connected on the same page
JavaScript
106
star
10

nodejs-push-notification-server

Simple nodejs push notification server using socket.io
JavaScript
76
star
11

riot-app-example

Simple riotjs isomorphic app
JavaScript
76
star
12

animore

1kb script that will make your DOM state transitions smoother & easier
JavaScript
72
star
13

erre

Modern, performant and tiny (~0.5kb) streams script using generators for browsers and node.js
JavaScript
30
star
14

nodejs-arduino-example

Simple example to show how to use arduino to communicate digital signals to a nodejs server
JavaScript
25
star
15

cmt

Get rid of all the boring git commit messages like 'small fix'. cmt is an automatic git commit generator
JavaScript
18
star
16

site-under-construction

Simple splash page to use while a site is still under construction
CSS
17
star
17

fcf

Monadic Functional Control Flow Micro-Library for Javascript/Typescript
TypeScript
14
star
18

rawth

Pure functional isomorphic router based on streams
JavaScript
14
star
19

fortytwo

List of interactive prompt helpers to pimp the UIs of your haskell programs
Haskell
12
star
20

reactive-libs-bench

Benchmark tests to check some popular reactive libraries - raw tests no DOM involved
JavaScript
11
star
21

days-since-last-javascript-drama

Timer to track how many days passed since the latest Javascript drama
HTML
9
star
22

riot-preset-babel-test

JavaScript
8
star
23

threejs-amd-bootstrap

Simple and lite threejs bootstrap project build using requirejs
JavaScript
7
star
24

nisiu

Password manager written in Riot.js
HTML
6
star
25

i-got-ya

Stop fucking with my machine when I am not here :)
Go
6
star
26

the-cost-of-native-javascript-modules

The performance cost of native javascript modules
HTML
5
star
27

immutable-dom

Lock any HTML DOM Element 🔐
JavaScript
4
star
28

ruit

Functional tasks serialization mini script (0.3kb)
JavaScript
4
star
29

jquery-fitToParent

A jQuery plugin for proportionally scaling an element to its parent element. Useful for thumbnails and avatars.
JavaScript
4
star
30

package-requirejs

Generate a valid requirejs-config.js file automatically detecting all the package.json dependencies
JavaScript
4
star
31

riot-now-demo

Simple now riot component
3
star
32

Caronte.js

Simple javascript ajax file uploader script made for the modern browsers
JavaScript
3
star
33

EasyAnimationFrame

Using EasyAnimationFrame.js you can make html5 canvas or html animations having the complete control over the framerate.
JavaScript
3
star
34

riot-chrome-extension

riojs chrome extension example
JavaScript
3
star
35

rob

Personal projects generator written in haskell
Haskell
3
star
36

smell-of-funtional-programming

Demo code for the "Smell Of Functional Programming" presenation
JavaScript
3
star
37

SublimeText2-Parallel-Builder-Plugin

Sublime Text2 Plugin - It allows you to run more than one build command in Parallel
Python
3
star
38

gianlucaguarini.github.io

gianlucaguarini.github.io original source
HTML
3
star
39

cumpa

Minimal function composition implementation
JavaScript
2
star
40

riot-ssr-now

Simple riot example to demonstrate how to deploy on zeit.co
JavaScript
2
star
41

curri

Minimal curry implementation
JavaScript
2
star
42

mail-client-ui-demo

Simple Mail Client Demo with ~20 Javascript code
JavaScript
2
star
43

grunt-testardo

grunt plugin to test your project using testardo
JavaScript
2
star
44

html5-semantic

Meaningful HTML5 Designs - webzurich - 25.05.2020
2
star
45

the-colour-of-the-moment

Random colored image generator service written with go
Riot
2
star
46

executor

Haskell module to execute single or multiple shell commands
Haskell
1
star
47

amp-vs-pwa-vs-HTML

Simple Benchmark to check the speed of 3 different website types
HTML
1
star
48

the-fly

JavaScript
1
star
49

OSXKeyboard

Go helper script to detect the global keypress events on OSX
C
1
star