I’ve been playing with Modernizr in WordPress lately and here’s something that i’m trying to achive.
I’m adding an HTML5 input field in a custom options page, specifically:
<input type="number" />
Now with Modernizr i can test if the browser supports it like this:
Modernizr.load([
{
test: Modernizr.inputtypes.number, // this is the test
nope: [ load js,css etc ] // if it's not supported load your custom lib
complete: function() {
// do something
}]);
Would it be possible instead of manually loading my custom libraries in the “nope” method, to invoke the wp_register_script()/wp_enqueue_script functions?
I mean i could just register/en-queue my custom libraries but i’d like to do that only if it’s necessary (if the browser doesn’t have native support for this kind of input type.
Any ideas?
Please check the example of Alex Hempton-Smith, maybe its help you.
I would do it in this way (admittedly, it’s not ideal, but I think its probably the easiest to read).
Create an array of all the conditional scripts you may need, and pass them to javascript using
wp_localize_script
(in 3.3 on, you will be able to use the much more logical aliaswp_add_script_data
as discussed here).Then just access those variables in your Modernizr tests: