Coder Social home page Coder Social logo

yepnope.js's Introduction

#Yepnope JS#

Regressive Enhancement With Style.

A small wrapper around LABjs to help use feature detection to load exactly the scripts that your user needs, not just all the scripts that you think they might need.

More docs, etc at: http://yepnopejs.com

##A simple example (assuming modernizr is there):##

yepnope([
  {
    test : Modernizr.indexeddb,
    yep  : ['/js/indexeddb-wrapper.js', '/css/coolbrowser.css'],
    nope : ['/js/polyfills/lawnchair.js', '/js/cookies.js', '/css/oldbrowser.css']
  }
]);

##Common Use:##

###The wait Flag### Forcing a wait between all dependencies. The wait flag will ensure all items within the group execute in order, as well as before all subsequent groups. In this example: jquery.js will execute before anything in the next group, and googleapis.js will execute before needs-googleapis.js. The wait flag is more of a global way to force order.

yepnope([
  {
    load: 'js/jquery.js',
    wait: true
  },
  {
    test: Modernizr.geolocation,
    nope: ['googleapi.js', 'needs-googleapi.js'],
    wait: true
  }
]);

###The wait! prefix### Adding the wait! prefix to your scripts will ensure that anything listed after that script will wait for it. This includes within a single array of script strings: In this case, only polyfill.js has the wait! prefix, so needs-polyfill.js and doesnt-need-anything.js will execute after polyfill.js but may execute in different orders, between the two of them. The less wait calls you have, the faster your code may end up (as a general rule).

yepnope([
{
  test: Modernizr.borderradius,
  nope: ['wait!polyfill.js', 'needs-polyfill.js']
},
{
  test: Modernizr.multiplebgs,
  nope: ['doesnt-need-anything.js']
}
]);

##A crazy/contrived example:##

yepnope([
  // straight up load (using a force css prefix)
  'css!http://yayquery.com/css/base.css?alwaysload',
  
  // no tests, just a load and a callback
  {
    load:'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js',
    callback: function(){
      console.log('jQuery was loaded!');
      window.oldalert = window.alert;
      window.alert = function(){};
    }
  },
  
  // conditional test based loading, with a default load
  {
    // The test
    test: Modernizr.csstransforms && Modernizr.csstransforms3d,
    
    // If it passes, 'yep' is completely optional, so let's not load anything...
    
    // If it fails
    nope: [
      'http://github.com/madrobby/vapor.js/raw/master/vapor.min.js',
      'css!http://yayquery.com/css/base.css?supports3d=false' //prefix with css! if it doesn't end in .css
    ],
    
    // Load it no matter what
    both: 'http://www.json.org/json2.js',
    
    // For each thing loaded
    callback: function(id, testResult) {
      
      // check for the load of json2, specifically
      if (id === 'http://www.json.org/json2.js') {
        window.alert = window.oldalert;
        console.log('bypassed crock\'s alert in json2 yo, because the test result was: ', testResult);
      }
    }
  }
]);

Any forks and stuff are welcome.

##Current Version##

0.2.3 (Note LABjs is in a bit of flux with some new browsers, so check the labjs repo for changes there. I'll keep on top of it as well.)

##License##

All of the yepnope specific code is under the WTFPL license. However, LABjs is subject to it's own license (currently BSD or MIT)

##Thanks##

Kyle Simpson - He is the creator of LABjs of which is fantastic. All of the actual loading in yepnope uses LABjs under the covers, so that's all him.

Paul Irish - Thanks or whatever.

yepnope.js's People

Contributors

slexaxton avatar davidpadbury avatar

Stargazers

 avatar

Watchers

 avatar James Cloos 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.