• Khan Aqib

    (@kashmirwordpress)


    Hi https://wordpress.org/support/profile/bethannon1

    i have a myscrits.js file in which i call all the scripts api.
    Need some help how can i check the current template in js file so that below code executes only at that time

    // DOM Ready
    $(function() {
    
          //some code is here
    
        // the below code i want this is execute only for home page only
       //  as i enqueued files related to below script in proper
      //   condition in functions.php 
    
               var button = document.getElementById('cn-button'),
               wrapper = document.getElementById('cn-wrapper');
    
              //open and close menu when the button is clicked
    	      var open = false;
    		  button.addEventListener('click', handler, false);
    
    				  function handler(){
    				    if(open){
    				      this.innerHTML = "<div class='logo '></div>";
    				      classie.add(wrapper, 'opened-nav');
    				    }
    				    else{
    				      this.innerHTML = " <div class='logo'></div>";
    				    classie.remove(wrapper, 'opened-nav');
    				    }
    				    open = !open;
    				  }
    				  function closeWrapper(){
    				    classie.remove(wrapper, 'opened-nav');
    				  }
    
           });
    
        });
    
    });
    
     // some other code is also here
Viewing 3 replies - 1 through 3 (of 3 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Need some help how can i check the current template in js file

    Use jquery to grab the <body> classes

    Thread Starter Khan Aqib

    (@kashmirwordpress)

    i will explain more in detail.

    the script i post is to be executed in home page only.

    and i have a myscripts.js file in which i place api calls to their respective js files. e.g bootstrap carousel api for controlling carousel things like interval.

    when i try to go e.g on contact us page which is placed in toogle container(means the container has jQuery toggle class).
    the toggle function gets effected due to above posted script.

    How can i control the above posted script by placing in the myscripts file.(Controlling means how can i check in js file which template is loaded.)

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Are you familiar with jQuery? You can check if a class exists on an element and then fire your code if it does, e.g.:

    if(jquery('.home').length !== 0) {
    
        // api calls to respective js files
    
    }

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Loading js scripts by check page inside js file’ is closed to new replies.