Best C5 practice for conditional execution of external JS within block edit.php file?
Permalink
C5 normally includes any javascript code contained inside external fs files found inside the js subdirectory off the block root directory.
In my case I am using a JQuery plugin called placeholder.js to put faint text inside form input fields.
At the bottom of my edit.php file I have the following to execute the plugin code.
All well and good. It works.
But...some browsers already support the placeholder input element natively and for those browsers there is no sense having the plugin javascript loaded into memory or executing said Javascript.
So...the question is how best to load and execute the external plugin only for those browsers that do not support the new HTML5 placeholder attribute?
As far as conditional execution I have come up with the following to be placed at the bottom of my edit.php file...
I don't really know if the above code works since my browser supports the placeholder attribute natively but regardless...I am mainly wondering about the logic of doing it this way.
Is this done in the C5 way? I mean conditional execution JS code being put at the bottom of edit.php?
Additionally is there a C5 best practice to prevent even the automatic loading of the external plugin from inside the js subdirectory when a browser doesn't even need it?
Carlos
In my case I am using a JQuery plugin called placeholder.js to put faint text inside form input fields.
At the bottom of my edit.php file I have the following to execute the plugin code.
$('input[placeholder], textarea[placeholder]').placeholder();
All well and good. It works.
But...some browsers already support the placeholder input element natively and for those browsers there is no sense having the plugin javascript loaded into memory or executing said Javascript.
So...the question is how best to load and execute the external plugin only for those browsers that do not support the new HTML5 placeholder attribute?
As far as conditional execution I have come up with the following to be placed at the bottom of my edit.php file...
<script> function PlaceholderSupported() { var input = document.createElement("input"); return ('placeholder' in input); } if(!PlaceholderSupported()) $('input[placeholder], textarea[placeholder]').placeholder(); }); </script>
I don't really know if the above code works since my browser supports the placeholder attribute natively but regardless...I am mainly wondering about the logic of doing it this way.
Is this done in the C5 way? I mean conditional execution JS code being put at the bottom of edit.php?
Additionally is there a C5 best practice to prevent even the automatic loading of the external plugin from inside the js subdirectory when a browser doesn't even need it?
Carlos