Thursday, 25 November 2021

Initialize js function on the page

I have a js file where there is a function with multiple parameters

function initSlider(slider, sliderRow, slides, CLONES_COUNT, doAutoPlay) {
. . .
}

And I want to use this function on different pages, but with slightly different parameters, let's say for one of the pages there will be

initSlider(slider, sliderRow, slides, (slides.length < 2) ? 0 : 1, slides.length > 1)

The question is, how do I call this initialization function on the page itself?

The usual page for laravel, blade.php where at the end there is a section with scripts

@section('scripts')
<script src="/js/slider.js"></script>
@endsection

I tried like this

@section('scripts')
<script src="/js/slider.js"></script>
<script>
   $(document).ready(function(){
        initSlider(slider, sliderRow, slides, (slides.length < 2) ? 0 : 1, slides.length > 1);
   });
</script>
@endsection

I seem to be doing everything right, but I get errors

jQuery.Deferred exception: initSlider is not defined ReferenceError: initSlider is not defined at HTMLDocument. (https://ift.tt/3cFmKB1) at e (https://ift.tt/3xgtmPS) at t (https://ift.tt/3nJdT7O) undefined

Uncaught ReferenceError: initSlider is not defined at HTMLDocument. (article_1:730) at e (jquery-3.6.0.min.js:2) at t (jquery-3.6.0.min.js:2)



from Initialize js function on the page

No comments:

Post a Comment