• Hello

    I would like to include a .js in the Header or Footer of your theme but you already added the script tag so it’s hard for me to include the js file.

    <script src="/mycode.js"></script>

    will not work and always get error: Unexpected token <

    I tried the code:

    var script = document.createElement('script');
    script.src = "/myfile.js";

    But it does not work as well.

    Please advise.
    Thanks

Viewing 1 replies (of 1 total)
  • Hi,
    You can add custom scripts in multiple ways.

    1. The code you applied is almost okay. Please try this code for perfect output:

    var script = document.createElement('script');
    script.src = "https://yourdomain.com/filepath/myfile.js";
    document.head.appendChild(script);

    2. Copy the code from your “myfile.js” and paste it in Customizer->Custom Javascript->Header Script/Footer Script. You don’t need to use any <script></script> tag.

    3. You can Enqueue JS script in WordPress way. Create a child theme from https://flexia.pro/child-theme and activate the theme. Open functions.php file from the theme folder. Add this php code:

    wp_enqueue_script(
        'myfile-js',
         get_stylesheet_directory_uri() . 'myfile.js',
         array( 'jquery' ),
         1.0.0
    );
Viewing 1 replies (of 1 total)
  • The topic ‘How to include a .js in header or footer of your theme?’ is closed to new replies.