How to find out if user is logged in and insert some js

Hope someone can help me with this.

I want to insert a getsitecontrol.com script to ask our members to subscribe to a newsletter. But I want this only to be shown for members.

I tried doing this using jQuery in Admin > Customize > CSS/HTML >

<script>
$(document).ready(function(){
if((Discourse.User.current())) 
{ $('<script>')
    .attr('type', 'text/javascript')
    .text('(function (w,i,d,g,e,t,s) {w[d] = w[d]||[];t= i.createElement(g);
    t.async=1;t.src=e;s=i.getElementsByTagName(g)[0];s.parentNode.insertBefore(t, s);
  })(window, document, '_gscq','script','//widgets.getsitecontrol.com/xxxx/script.js');')
    .appendTo('body');
}
else { //Do nothing or show someother message
}});
</script>

But it doesn’t seem to work. No errors on the console too. Can someone help please?

You are not escaping the quotes in <script> tag text (you need to), you have an extra closing brace }.

3 Likes

Thank you for pointing out, that fixed it. Is it possible to get the trust level of a user using javascript?

That question would be best asked in a new topic

Discourse.User.current().trust_level

2 Likes