Link to home
Start Free TrialLog in
Avatar of APD Toronto
APD TorontoFlag for Canada

asked on

Exclude JS From Running on 2 Body Classes

Hi Experts,

I'm working on www.iwscc.ca and I would like a block of code to run on all pages, except https://iwscc.ca/blog-posts/ (page-id-2956) and for all posts that derive from it (single-post).

I have started the following:
        
        if (!($('body').hasClass('page-id-2956')) || !($('body').hasClass('single-post'))){
            console.log('embed');
        }
        

Open in new window


The issue is that this works on the Blog page (ie- "embed" is not logged), but it doesn't on the posts (ie- "embed" is logged).

What am I doing wrong?
Avatar of lenamtl
lenamtl
Flag of Canada image

First you have error in the console
Chrome : right-click / inspect

 I can see embed on both page, maybe clear your browser cache..
I think you have your logic mixed up. You are saying run this code if the page does not have 'page-id-2956' or it doesn't have 'single-post'. If I am understanding correctly you want it to be this page does not have 'page-id-2956' or 'single-post.  Something like below.

if (!(($('body').hasClass('page-id-2956')) || ($('body').hasClass('single-post')))){
   console.log('embed');
 }

Open in new window

Avatar of APD Toronto

ASKER

@Jeffery, I don’t Understand.


I want the code NOT to run if any page has either of those classes 
use this one

https://jsfiddle.net/HainKurt/e5x7w2ah/


if (!(($('body').hasClass('page-id-2956')) || ($('body').hasClass('single-post')))) {
 console.log('embed');
}

Open in new window

Yes, that is why I moved the ! (or not) outside the parenthesis.  The way you had it written before it was still running the code within the if block if either class was not present.  You only wanted it to run if both are not present.
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial