• Hi, I build a blog based on bootstrap and want to enable JS. This is my code in the function.php :

    function twentySven_register_scripts() {
    
    	wp_enqueue_script( 'twentySven-js', get_template_directory_uri() . '/assets/js/script.js');
    }
    
    add_action( 'wp_enqueue_scripts', 'twentySven_register_scripts' );

    This is my file structure:

    themes
    -twentySven
    –assets
    —js
    —-script.js

    Thank you for your help ??

    Best,

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @presse2009,

    Change the file name from ‘function.php’ to ‘functions.php’.

    Thanks

    Thread Starter presse2009

    (@presse2009)

    @vishakha07 Thank you. But it does not work ??

    Hi @presse2009,

    Replace code with the given one

    function twentySven_register_scripts() {
    
        wp_enqueue_script( 'twentySven-js', get_template_directory_uri() . '/assets/js/script.js', array('jquery'), '1.0.0', true);
    }
    
    add_action( 'wp_enqueue_scripts', 'twentySven_register_scripts' );

    and if you are using child theme the use ‘get_stylesheet_directory_uri()’ instead of ‘get_template_directory_uri()’.

    Thanks

    Thread Starter presse2009

    (@presse2009)

    Hi @vishakha07 Thank you. I changed it. Hmmm, it does not work either. You are refering to a jquery dependency which I do not understand. (Maybe the keyword “Bootstrao” was misleading.) It’s just the Bootsrap css. The script is vanilla js –?in fact is this:

    window.addEventListener("DOMContentLoaded", () => {
      let scrollPos = 0;
      const mainNav = document.getElementById("mainNav");
      const headerHeight = mainNav.clientHeight;
      window.addEventListener("scroll", function () {
        const currentTop = document.body.getBoundingClientRect().top * -1;
        if (currentTop < scrollPos) {
          // Scrolling Up
          if (currentTop > 0 && mainNav.classList.contains("is-fixed")) {
            mainNav.classList.add("is-visible");
          } else {
            console.log(123);
            mainNav.classList.remove("is-visible", "is-fixed");
          }
        } else {
          // Scrolling Down
          mainNav.classList.remove(["is-visible"]);
          if (
            currentTop > headerHeight &&
            !mainNav.classList.contains("is-fixed")
          ) {
            mainNav.classList.add("is-fixed");
          }
        }
        scrollPos = currentTop;
      });
    });

    So I would think I can skip the dependency part. I am sorry, eating your time. Cheers and thank you again.

    Moderator bcworkz

    (@bcworkz)

    Yeah, no jQuery there. We’re kind of guessing at the problem because it’s unclear where the problem lies.

    Does the file reference appear in your page’s source HTML?
    If so, does the link correctly lead to the file? When you follow the link, do you see your JS code?
    If that checks out, it’s probably not working due to a script error (not necessarily yours, any error can prevent others from working). Check your browser’s console for errors.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get JS not registered’ is closed to new replies.