Giter Site home page Giter Site logo

conditionizr's Introduction

conditionizr.js Build Status

Detects front-end environments and binds the results to the conditionizr Object. <1KB module for async callbacks, conditional script/style loading, automated polyfilling, inline boolean evaluations, classNames for styling overrides, custom tests and more.

Read the developer documentation.

By @toddmotto and @markgdyr.

Installing with Bower

Use the repository hook:

bower install conditionizr

Core and methods

The Conditionizr core is made up of several public methods.

.config()

The config API allows you to easily configure your conditional environments, once tests are added. You have a choice of loading conditional scripts, styles and class names per config test, as well as specifying an asset path to where your files are.

conditionizr.config({
  assets: '/path/to/my/assets/',
  tests: {
    'safari': ['script', 'style', 'class']
  }
});

This would then load browser specific tweaks, or you could use the global class override:

<html class="safari">
  <head>
    <script src="assets/conditionizr/safari.js"></script>
    <link href="assets/conditionizr/safari.css" rel="stylesheet">
  </head>
</html>

.add()

Custom tests can be passed into the Conditionizr core and used with all APIs, making your conditional coding seamless. Conditionizr will handle all the hard work for you, you just need to provide it a test that returns a boolean, true/false. This can be done via a function callback or inline Boolean.

Inline syntax
conditionizr.add('safari',  /constructor/i.test(window.HTMLElement));
Function syntax
conditionizr.add('safari', function () {
  return /constructor/i.test(window.HTMLElement);
});

.on()

Using .on() you can create custom callbacks for when conditional tests return true which are your best bet if you can avoid loading an external script and style, for instance if I’ve added a test for Safari, when a user is running Safari, your callback will run. This is preferred as it saves an HTTP request and improves performance.

conditionizr.on('safari', function () {
  // safari
});

You can also ignore environment tests using !:

conditionizr.on('!safari', function () {
  // anything but safari
});

Conditionizr returns an object for you to also test environment states inside expressions.

if (conditionizr.safari) {
  // safari
}

.polyfill() and .load()

Polyfill and load each allow you to inject custom assets based on a conditional test. All you need is the external URI, and your predefined conditional tests to declare.

conditionizr.polyfill('//html5shiv.googlecode.com/svn/trunk/html5.js', ['ie6', 'ie7', 'ie8']);

Using the .load() method instead of .polyfill() is purely for naming conventions to differentiate between polyfills and generic assets.

conditionizr.load('//cdnjs.cloudflare.com/ajax/libs/hammer.js/1.0.5/hammer.min.js', ['ios']);

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Gulp.

Release history

  • 4.5.1
    • Improve regular expression for ignoring an environment
  • 4.5.0
    • Add new options inline Boolean syntax, pass tests in inline or as callback function
  • 4.4.0
    • Rewrite Jasmine unit tests, Karma spec runner, better testing
    • Convert to Gulp.js
    • Finish missing load/polyfill() and on() Jasmine unit tests
    • Modular rewrite, reduce bloat
    • Performance enhancements, less loops, clutter, reduction in size
    • Enhance private load() method for faster loading
    • Breaking change: No add dependency array, config only
  • 4.3.0
    • Add CommonJS/Browserify
    • AMD bugfix
  • 4.2.0
    • Add AMD support
  • 4.1.0
    • Performance enhancements for .on() and .add()
    • Bugfix for .on(!) callbacks
    • Improve _loader() performance
  • 4.0.0
    • Separate tests/detects from core
    • Performance rewrites
    • Add .on(), .add(), .config(), .load(), .polyfill() methods
    • Ship a public Object for boolean tests
    • Add /detects directory which hosts all detects
  • 3.0.0
    • Rewrite Conditionizr with improved IE detects
  • 2.0.0
    • Remove jQuery dependency
  • 1.0.0
    • Initial release

conditionizr's People

Contributors

dark-panda avatar dslatten avatar gisu avatar johnantoni avatar markgoodyear avatar toddmotto avatar toddskinner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

conditionizr's Issues

IE9 & IE10 detects - JSHint escaping error

When I use detects for IE9 & IE10 to add class names and then run the file through JSHint (via grunt-contrib-jshint), I get the error 'bad or unnecessary escaping'. I have circumvented this error by adding a second \ after MSIE 10\ and MSIE 9\ which, in context, is:

conditionizr.add('ie10', ['class'], function () {
  return (Function('/*@cc_on return (/^10/.test(@_jscript_version) && /MSIE 10\\.0(?!.*IEMobile)/i.test(navigator.userAgent)); @*/')());
});

conditionizr.add('ie9', ['class'], function () {
  return (Function('/*@cc_on return (/^9/.test(@_jscript_version) && /MSIE 9\\.0(?!.*IEMobile)/i.test(navigator.userAgent)); @*/')());
});

It appears to output class names OK in both browsers. I'm just wondering: should I expect any adverse results from doing this?

Returning a JS object containing all computation

Hey there,

Having all CSS classes is really nice, but sometime I would like to test some conditions also in my JS. I can do something like document.documentElement.className.indexOf('chrome') > -1, but it's a bit ugly...

I'm not sure that's the goal of Conditionizr, but since all computation will be done anyway, wouldn't it be nice for the conditionizr function to return an object containing all the flags? Like:

{
  chrome: true/false,
  ...
  ie6: true/false,
  retina: true/false,
  ...
  linux: true/false
}

Fully backward compatible. Just do var c = conditionizr(...) and you're ready to go! I can do a PR if you like the idea, just wanted to be sure it would fit in it or not...

Conditionizr for ipad and iphone

Thanks for creating this super stuff like Conditionizr -
I just wanted to know, Is there any class for ipad and iphone browser?

Touch Support

I personally love Modernizr, but I use Conditionizr for the browser classes, it works great! But I'm unsure on the effectiveness of the touch test.

However now that touch events have changes from v2 to v3 are you going depreciate or improve on the current touch.js test?

Device Pixel Ratio?

Is there a way to detect device pixel ratio?

Currently, it's either retina/no-retina and when I'm testing on Opera Mobile Emulator most of them return as no-retina even though it has a 1.5 pixel ratio.

I'm looking for something like:
retina-1.3
retina-1.5
retina-2 (or this can be the global "retina")

"main" in bower file

Tried installing conditionizr through bower. I noticed you don't have a "main" specified. It should point to conditionizr.js. Not usable in bower automated systems (like gulp). This is in no way a bug or anything, just a gentle reminder :)

npm

Could this be packaged as an npm module?

Rails Asset Pipeline

Is there a way to specify scripts to load using conditionizr but when they are in the asset pipeline provided by rails? How would they be specified. What file path would I use?

No CSS classes

I can't seem to get the CSS classes to be added to the head element with the new version 4.0.0. I am using the config API as follows:

conditionizr.config({
  tests: {
    'chrome': ['class'],
    'safari': ['class'],
    'firefox': ['class'],
    'ie8': ['class']
  }
});

The add() API appears to work, giving me the expected true/false result, but it's the class names I need. For now, I'm going back to an older version which does the job.

HTML5 Shiv works only after 1st refresh in IE6,7,8 [workaround]

For those who using the .polyfill() API to load HTML5Shiv, in IE6,7,8 the HTML5Shiv will only work on refresh (and every other refresh, just initial load doesn't fire the shiv fast enough it seems...)

We have a workaround for this while we investigate, but adding an empty IE conditional to the <head> solves this:

<head>
  <script src="conditionizr.js"></script>

  <!-- other code -->

  <!--[if IE]><![endif]-->
</head>

Note the empty IE conditional - this weird IE quirk forces all polyfills to load. We're going to look into doing this dynamically, but for now you can sit tight with our small workaround.

ieLessThan and multiple customScript

Guys,
i just struggled a bit to discover that to add multiple custom scripts to the ieLessThan parameter you have to get rid of the http: part of the url like so:

customScript: '//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.6.1/html5shiv.js,//css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js'

Not really an issue but maybe a clearer explanation in the doc would be helpful.

Add no-js/js

Would like to suggest to add this class if Modernizr is not in use.

Typos in documentation

If think there is a few typos in the documentation:

  • "This would then dynamically both polyfills," should be "This would then dynamically load both polyfills,"
  • "platform, which again you can is set to" should be "platform, which can be enabled if set to" or something like that.

Version 4 doesn't add browser classes to <html>

First off, conditionizr is amazing. I've been using version 3 for a few months and love it.

I just upgraded to version 4 but the browser classes have disappeared from my html tag.

I don't need anything fancy like loading conditional scripts, stylesheets, etc. I just need it to add the browser class to the html tag.

I read through your docs, specifically your Basic Setup, but am still stuck. How can I get version 4 to add the browser class to the html tag?

Here's my Gist: https://gist.github.com/ryanburnett/8030408

MIT license

Hi, can you add an MIT-LICENSE.txt file to the project. I'd add it myself but it's not my baby.

appreciate it

Conditionizr v5.0

I'd like to discuss the future API for Conditionizr, topics of discussion:

Detects

I think we should move away from just "environment" detects, and promote the fact users can add feature detects and not just environment detects, so we should include /features/ directory with things like cssTransforms.js inside to get the best of both worlds for the end developer.

Asynchronous file loading

Currently we can load JS/CSS files, but we have zero information as to when the request has finished so that the developer can instantiate plugins/scripts that rely on the conditional loading. This is fine for CSS, however for JS async files need instantiating once available.

Possible ways to do this:

// has an anonymous function as the callback
conditionizr.load('/fastclick.js', 'ios', function () {
    // fastclick.js has loaded
});

// namespace the event and instantiate when available
conditionizr.load('fastclick', '/fastclick.js', 'ios');

conditionizr.on('fastclick', function () {
    // fastclick.js has loaded
});

I'm not sure which I prefer currently, but this seems crucial to our next feature integration.

We could also look at the top level config Object as a host for loading all these async files:

conditionizr.config({
  assets: '/path/to/my/assets/',
  load: {
    'fastclick': { // use the prop name as event namespace
        file: '/fastclick.js',
        run: ['ios', 'safari']
    }
  }
  tests: {
    'safari': ['script', 'style', 'class']
  }
});

conditionizr.on('fastclick', function () {
    // fastclick.js has loaded
});

Other points to discuss, should we deprecate/merge the ['script', 'style', 'class'] feature in favour of this new file syntax. I'm not sure how I feel about the script/style loading feature, however I love the class capability of conditionizr so would want to keep this.

@nfq @markgoodyear

Default settings

Initializing the script without options (using a plain call to conditionizr()) does not add the browser class names to the html element.

With no options, the default settings should be used, but they are being set incorrectly with baseSettings[0] instead of baseSettings.

i.e. In conditionizr.js, line 28 which reads

chrome: baseSettings[0],

should be changed to

chrome: baseSettings,

and similarly for lines 29-37.

IE9, IE8 HTML Class not being added?

Hi,

For some reason I am not getting .ie9, and .ie8 classes added to the html element. I am testing using IE10 on Windows 7, via VirtualBox, and using F12 Dev tools to choose IE9 and IE8.
.ie10 shows up great though.

Just wondering why this is?

EDIT: I've also tried browserstack with no luck.

Thanks

bug!? different result with X-UA-Compatible and ieLessThan

my html source code like this:

<head>
<meta http-equiv="X-UA-Compatible" content="IE=7"/>
<script>
...
ieLessThan: { active: true, version: '8', classes: true, ........................},
...
</script>
</head>

and then get different result with ie10 and ie8:
ie10: class="lt-ie8 ie7 ..."

<html class=" lt-ie8 ie7 no-retina no-touch win" id="conditionizr" xmlns="http://www.w3.org/1999/xhtml">

ie8: class="ie8 ..."

<html class=" ie8 no-retina no-touch win" id="conditionizr" xmlns="http://www.w3.org/1999/xhtml">

Dont get Classes for IE10 and IE9

Hi,
i have a Problem with the Browser detection for IE10 and IE9. I have combined the Conditionizr Script with the Test files. Firefox, Safari, Chrome and IE11 is fine. IE10 and IE9 dont have a Browserclass.

Whats the Issue here? Do is need something special for IE10 and 9? (I have tested with a real IE10 and IE11)

$(window).load(function() {
  conditionizr.config({
    tests: {
      'ie11': ['class'],
      'ie10': ['class'],
      'ie9': ['class'],
      'ie8': ['class'],
      'chrome': ['class'],
      'firefox': ['class'],
      'safari': ['class']
    }
  });
});

Make Conditionizr more like Modernizr.

with Modernizr the one thing that makes it great is the fact you don't need jQuery.

if you guys need help porting things over to regular js I can help, also I see a lot of N+1 cycles that could be cleaned up.

Fallback for CDN hosted scripts

What's the best approach here? I'm using conditionizr to load html5shiv, respond.min.js, and selectivizr-min.js from cloudflare's cdnjs for lte-ie9, and retina.js for retina. Will conditionizr add a conditional loader specifically for fallbacks?

Does conditionizr.js have to be loaded in the <head>?

According to the conditionizr documentation:

After downloading Conditionizr, place the main script inside the <head> tag, near the top of your scripts so it will run first. We then recommend running your initiating script directly after your main JavaScript files using the conditionizr(); to call.

I have a few questions about this...

  1. Does conditionizr.js have to be loaded in the <head>?
  2. Can it be placed right before </body> instead?
  3. Can it be dynamically loaded after the page load event (i.e., lazy-loaded)?

Mobile detection doesn't work with BrowserStack emulated devices

...as the title says.

BrowserStack responded:
"The test fails because the the user agent supplied in case of android emulators is not the device name but the string “android emulator”, and similarly in case of iOS devices also the user agent passed is iOS simulator."

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.