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
im try
@yield('scripts')
@stack('slider-script')
</body>
@section('scripts')
other scripts
@endsection
@push('slider-script')
<script src="/js/slider.js"></script>
@endpush
@push('slider-script')
<script>
initSlider(slider, sliderRow, slides, (slides.length < 2) ? 0 : 1, slides.length > 1);
</script>
@endpush
but nothing works, error
Uncaught ReferenceError: initSlider is not defined
CodePudding user response:
If Phil's answer doesn't work. There is one possibility: You haven't declared @yield('script')
anywhere in your document. @yield
goes with @section
and @stack
goes with @push
. Don't let them confuse you.
CodePudding user response:
You could put all Javascript code between @section('script')
and @endsection
.
slider.js
function initSlider(slider, sliderRow, slides, CLONES_COUNT, doAutoPlay) {
console.log(slider);
}
blade
@section('script')
<script src="/js/slider.js"></script>
<script>
slides = [1,2];
initSlider(1, 2, slides, (slides.length < 2) ? 0 : 1, slides.length > 1);
</script>
@endsection
If you want to use stack
method:
In your layout php file (e.g.
app.blade.php
)@stack('scripts') </body> </html>
In your current blade file:
@push('scripts') <script src="/js/slider.js"></script> @endpush @push('scripts') <script> slides = [1,2]; initSlider(1, 2, slides, (slides.length < 2) ? 0 : 1, slides.length > 1); </script> @endpush
Either way should work.