简体   繁体   中英

How do I hide a opt-in script from logged in users?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM