The same .js file outputting multiple times from different block templates

Permalink
Hi There,

We have a slider that is used with two different block templates but when both templates are used on the same page, the .js initialisation file loads twice from two different locations:
/blocks/page_list/templates/related_pages/js/slider.js
/blocks/page_list/templates/slider/js/slider.js


We're thinking of putting the initialisation .js into the footer of every page and somehow using php to check the page for div.slider before outputting - can php do this or is it better to use jquery?

We just don't want the initialisation code to run unless div.slider is on the page.

Cheers

Ben

View Replies: View Best Answer
cmscss replied on at Permalink Best Answer Reply
In the end we just used jquery in footer.php to check if the slider or gallery divs are on the page - pretty simple!

<script>
$(document).ready(function() {
  // initialise slider if .owl-carousel is on the page
  if ($(".owl-carousel")[0]) {
    // initialisation code
  }
  // initialise fancy-box if .open-fancybox is on the page
  if ($(".open-fancybox")[0]) {
    // initialisation code    
  }
});
</script>