Git Product home page Git Product logo

afteronload.js's Introduction

AfterOnLoad.js

AfterOnLoad.js is a JavaScript snippet for queuing functions to be triggered on/after the window load event (aka window.onload).

When you are optimizing your page load times, it often helps to move the loading of certain JavaScript resources after the window.onload event. For example, delaying third party resources like social media widgets (Facebook, Twitter, Google+, ...) or even visitor tracking systems (Google Analytics, ...) until after window.onload can help to improve the perceived loading speed of your pages.

Design/features

AfterOnLoad.js has been designed with the following in mind:

  • Compact and intended for inline JavaScript. The code is optimized for compact minification, so that the snippet can be added in an inline JavaScript block in your markup. Setting up window.onload handlers is typically bootstrap activity, where you don't want to lose precious page load time on network IO from additional JavaScript file loading.

  • No framework dependencies or interference. Just raw, standard/common JavaScript.

  • Cross browser compatibility: window.addEventListener (Firefox, Webkit, Opera), window.attachEvent (IE) or fall back on window.onload piggybacking? We've got it covered.

  • Queuing also works after the load event has happened. If you call afterOnLoad(fun) after window.onload, the function will be triggered immediately, as there is nothing to wait for.

Usage

AfterOnLoad.js is intended to be used as an inline JavaScript snippet or as part of a bigger bootstrap script. It's just too small to be loaded as a separate script file.

Just copy the minified snippet in your bootstrap code:

afterOnLoad=function(a){var b="onload",c="addEventListener",d="attachEvent",e=!1,f=function(f){var g=a[b];e?f():a[c]?a[c]("load",f,!1):a[d]?a[d](b,f):a[b]=g?function(a){g(a),f(a)}:f};return f(function(){e=!0}),f}(window);

And optionally change the function name to suit your needs.

Queue one or more functions as follows:

function zazalo() {
    alert('We are after onload now.');
}

afterOnLoad(zazalo);

Licence

Do whatever you want. Stealing is bad. Attribution is nice.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

afteronload.js's People

Contributors

soxofaan avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.