Jump to content

Limit JS script loading to specific usergroups


Recommended Posts

One feature of my site is that there is limited free access to anybody who wishes to have it, but for unlimited access there is an annual subscription payable. For the "free" users (including guests), the tradeoff is that they are served adverts. I would like to ensure that the JavaScript file(s) that are loaded for the adverts are not even in the output HTML code for paid subscribers.

For example:

{{some template code to say "if user is not in Administrators, Moderators or Paid Subscribers groups, output this line"}}
<script type="text/javascript" src="/some/path/to.js"></script>
{{endif}}

How would I achieve this?

Link to comment
Share on other sites

Ah, so I'm getting an error saying there's an error in template includeJS which may be out of date, so I'm guessing $member isn't available there.

But if I try isolating just the guests:

<!-- Ad Testing -->
{{if !\IPS\Member::loggedIn()}}
<script type="text/javascript" src="script.js"></script>
{{endif}}
<!-- End Ad Testing -->

So my logic is if the member is NOT logged in, allow the <script> tag to be included.

That doesn't work - all I see is the comments wrapped around the if statement.

Link to comment
Share on other sites

  • 1 year later...
On 1/18/2022 at 6:24 PM, Steve Grant_189967 said:

Gotcha, thanks 🤦‍♂️

{{if !\IPS\Member::loggedIn()->inGroup(array(4,6,7))}}
<script type="text/javascript" src="script.js"></script>
{{endif}}

Now working 😎

Is this still the correct way to load a script to guests only?

Sorry to wake up an old thread, but i need to know. Thanks in advance

 

{{if !\IPS\Member::loggedIn()->inGroup(array(4,6,7))}}
<script type="text/javascript" src="script.js"></script>
{{endif}}
Link to comment
Share on other sites

Any of the following would likely work.

{{if \IPS\Member::loggedIn()->inGroup( array( \IPS\Settings::i()->guest_group ) )}}

{{if \IPS\Member::loggedIn()->member_group_id == \IPS\Settings::i()->guest_group}}

{{if !\IPS\Member::loggedIn()->member_id}}

Generally speaking, the third one is the most accepted way of determining if a user is a guest or not.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...