Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
Running javascript function from a js file

I have a Shopify website and I've added a .js file which holds few javascript functions, how can I tell a <script> which function to call on demand?

I've added the file to <head> like so:

<script src="/urls/for/custom-scripts.js" defer="defer"></script>

And I want to run different functions on different pages when the page is loaded, for example, inside main-product.liquid I want to run only ".aone-slide" and on blog.liquid I want to run only ".ul.accordion", how is this possible?

I've tried to do something like that:

<script>
  !function(o) {
    console.log("theme.liquid last </body> script -> DOMContentLoaded");
    o.addEventListener("DOMContentLoaded", function() {
      console.log("DOMContentLoaded event listener added successfully!");
      $(document).on('ready', function() {
        
      });
    });
  }(document);
</script>

But I don't know how to continue because the name of the function or the trigger is a div/css class.

This is the file:

(function($) {
    $(".aone-slide").slick({
        dots: !0,
        infinite: !1,
        arrows: !1,
        speed: 300,
        slidesToShow: 2,
        slidesToScroll: 2,
    }), $("ul.accordion").accordion(), $(".slider-for").slick({
        slidesToShow: 1,
        slidesToScroll: 1,
        arrows: !1,
        fade: !0,
        asNavFor: ".slider-nav"
    }), $(".slider-nav").slick({
        slidesToShow: 8,
        slidesToScroll: 1,
        asNavFor: ".slider-for",
        dots: !1,
        centerMode: !1,
        focusOnSelect: !0,
        responsive: [{
            breakpoint: 992,
            settings: {
                slidesToShow: 6,
                slidesToScroll: 1
            }
        }]
    })
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think you're confusing what a Javascript function is.

In your .js file you have only one function. This particular function is applying a slider (using the slick library) to different html elements. And it is doing that automatically when jQuery ($) is ready.

For example the first .slick call is creating the slider on the elements with class aone-slide.

So the first problem is this, if the .js is loaded (and the code you're showing seems to be correct) it should already work. It should already create the sliders, on all the pages. If it doesn't there is something missing (but I don't have enough elements to tell).

If you want to run slick only on the selected pages you should create functions inside your .js file and call them in the pages.

So your .js would become something like this

function slickAone(){
   $(".aone-slide").slick({
    dots: !0,
    infinite: !1,
    arrows: !1,
    speed: 300,
    slidesToShow: 2,
    slidesToScroll: 2,
    });
}

(without function ($)...)

and then in your page yout put

<script>
 function($){
    slickAone();
 }
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!