0

I run a membership site and have a script that shows an opt-in pop up form after visitors are on the page for more than 10 seconds. However, this pop-up shows up for public uses as well as our paid members.

How can I wrap the pop-up <script> in some custom JavaScript code to detect the presence of the "logged-in" body class? If it is found, the script should not fire.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
GlennMc
  • 1
  • 1
  • Possible duplicate of [How check if body has a specific class with JavaScript?](http://stackoverflow.com/questions/9532639/how-check-if-body-has-a-specific-class-with-javascript) – isherwood Apr 19 '16 at 18:58

1 Answers1

2
if (document.querySelectorAll('.logged-in').length) {
    // do your worst (here)
}

You can use querySelectorAll function to search for that DOM object on your page. and if it exists then run your code.


Might be better though to just have the backend handle this part and only load the js for your logged in users when your user is actually logged in

Tuvia
  • 859
  • 4
  • 15
  • Thanks Truvia, but I would actually need the exact opposite of this. If the .logged-in DOM exists, then DONT run the code. How would I do this? – GlennMc Apr 19 '16 at 20:05
  • Logical Not operator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_NOT – rlemon Apr 19 '16 at 20:35
  • 1
    Why this works: if statement checks against falsey so .length being anything greater than 0 is truthy. So checking .length for elements will pass the condition, if no element exist it fails it. Adding in the NOT converts the 0 to true (not falsey) and the positive numbers to false (not truthy). I hope this makes sense. – rlemon Apr 19 '16 at 20:37
  • @Tuvia Here is the script that I am trying to run for if the logged-in DOM is not present: How do I add the code solution you mentioned above? Unfortunatly I do not have access to the .jc file as it is hosted by LeadPages – GlennMc Apr 20 '16 at 18:37
  • @GlennMc handle that server side (since that is the side that should know if you are logged in or not. – Tuvia Apr 20 '16 at 21:34
  • @Tuvia Unfortunately I'm using a platform that doesn't give me access to server side scripts. I can only add scripting viea the tags. How can I accomplish this using that method? – GlennMc Apr 21 '16 at 16:00