There is a fragment with js connection whose task is to get the tabs variable from the front-page.php and custom-page.php

 var tabs = Array.prototype.slice.call(document.querySelector("#new-tab .carousel-indicators").children, 0); 

and for the rest of the villages, the receipt of this variable is not necessary, since This block is not on them and causes a script error. Interested in the question: how to properly test and connect this piece of code?

    2 answers 2

    I see several options:

    1. If these are built-in scripts, then enter them directly into the front-page.php and custom-page.php or make the function (method) visible from functions.php and call them in these files;
    2. If these are loadable scripts, then register them in functions.php and call them in files via enqueueScript() ;
    3. Alternatively, you can keep this script in a general file (for example, in a header), but check for the need to load via is_front_page() and check for the desired type of post;
    4. You can set the condition in the JS code itself.

    Like that:

     var element, tabs; element = document.querySelector("#new-tab .carousel-indicators") if (null !== element) { tabs = element.children; } 

    You can still do this:

     var tabs = document.querySelectorAll("#new-tab .carousel-indicators > *") 
    • These are not built-in scripts, so the last option worked - thanks! - Vasya

     var el = document.querySelector("#new-tab .carousel-indicators"); if (el !== null) { tabs = Array.prototype.slice.call(el.children, 0); //... и продолжение действий с этой переменной ... } 

    • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky
    • Thanks for the advice, it is fair. But what is interesting is that. I gave the first exactly the same answer and about the same level of commenting as the answer marked as correct. Not only is the answer ignored, it’s also zaminusovali. It looks like a small party. It is a pity that Stack Overflow does not provide mechanisms for ignoring users. I'll have to keep a file with users' nicknames, whose questions are never going to waste my time. - KAGG Design