I use a PHP based system to generate HTML pages from a template and series of included files.
Any scripts (whether linked or inline) are extracted from these includes and placed at the beginning or end of the generated page.
Inline scripts are combined into a single inline script.
This all works well.
The only thing I am missing is a way to combine the multiple $(document).ready jQuery functions into a single function.
Whilst it works fine having the multiple functions, for aesthetics and readibility it would be nice to have a single $(document).ready function.
To give an example, I'd want to be able to parse this:
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('.icheck').iCheck({
checkboxClass: 'icheckbox_minimal-green',
radioClass: 'iradio_minimal-green',
});
$('.icheckblack').iCheck({
checkboxClass: 'icheckbox_minimal',
radioClass: 'iradio_minimal',
});
});
$(document).on("eldarion-ajax:begin", function(evt, $el) {
$(".noData").remove();
$("#" + $el.data("bq")).show();
});
$(document).ready(function() {
$(".commentToggle").each(function(index) {
$(this).click(function() {
var tl_id = $(this).data('tl_id');
$("#commentForm" + tl_id).toggle();
});
});
});
$(function() {
$('#email_content').redactor();
});
</script>
Into this:
<script language="javascript" type="text/javascript">
$(document).on("eldarion-ajax:begin", function(evt, $el) {
$(".noData").remove();
$("#" + $el.data("bq")).show();
});
$(function() {
$('#email_content').redactor();
});
$(document).ready(function() {
$(".commentToggle").each(function(index) {
$(this).click(function() {
var tl_id = $(this).data('tl_id');
$("#commentForm" + tl_id).toggle();
});
});
$('.icheck').iCheck({
checkboxClass: 'icheckbox_minimal-green',
radioClass: 'iradio_minimal-green',
});
$('.icheckblack').iCheck({
checkboxClass: 'icheckbox_minimal',
radioClass: 'iradio_minimal',
});
});
</script>
I've seen similar questions which haven't really been answered, but have pointed in the direction of Tim Whitlock's jParser but this isn't very well documented and seems to be overkill for my needs.
I've considered using something based around preg_match_all, but I don't think JavaScript is consistent enough in its structure/formatting for this to be reliable.