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

**URL:** <https://meta.discourse.org/t/how-to-find-out-if-user-is-logged-in-and-insert-some-js/43492>\
**Category:** Product Support\
**Created:** [April 30, 2016, 7:11am UTC](https://meta.discourse.org/t/how-to-find-out-if-user-is-logged-in-and-insert-some-js/43492 "2016-04-30T07:11:25Z")\
**Posts on this page:** 6\
**Page:** 1

## AI-generated summary

[envieme](/t/-/43492/1) sought assistance with conditionally injecting a newsletter subscription script from getsitecontrol.com for logged-in users only. The initial attempt using jQuery in the Admin > Customize > CSS/HTML section failed silently. [DeanMarkTaylor](/t/-/43492/2) identified two issues: unescaped quotes within the `<script>` tag's text attribute and an extra closing brace. After applying these fixes, [envieme](/t/-/43492/3) confirmed the solution worked and asked if it was possible to retrieve a user's trust level via JavaScript. [DeanMarkTaylor](/t/-/43492/4) suggested creating a new topic for that question, but [Falco](/t/-/43492/5) provided the direct answer: `Discourse.User.current().trust_level`.

<div class="post-metadata">

**Author:** ![envieme](https://avatars.discourse-cdn.com/v4/letter/e/da6949/32.png) [@envieme](https://meta.discourse.org/u/envieme)\
**Post date:** [April 30, 2016, 7:11am UTC](https://meta.discourse.org/t/how-to-find-out-if-user-is-logged-in-and-insert-some-js/43492/1 "2016-04-30T07:11:26Z")

</div>

Hope someone can help me with this.

I want to insert a [getsitecontrol.com](http://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?

---

<div class="post-metadata">

**Author:** ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)\
**Post date:** [May 1, 2016, 5:14pm UTC](https://meta.discourse.org/t/how-to-find-out-if-user-is-logged-in-and-insert-some-js/43492/2 "2016-05-01T17:14:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![envieme](https://avatars.discourse-cdn.com/v4/letter/e/da6949/32.png) [@envieme](https://meta.discourse.org/u/envieme)\
**Post date:** [May 4, 2016, 12:39pm UTC](https://meta.discourse.org/t/how-to-find-out-if-user-is-logged-in-and-insert-some-js/43492/3 "2016-05-04T12:39:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)\
**Post date:** [May 4, 2016, 1:38pm UTC](https://meta.discourse.org/t/how-to-find-out-if-user-is-logged-in-and-insert-some-js/43492/4 "2016-05-04T13:38:05Z")

</div>

That question would be best asked in a new topic

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [May 4, 2016, 1:48pm UTC](https://meta.discourse.org/t/how-to-find-out-if-user-is-logged-in-and-insert-some-js/43492/5 "2016-05-04T13:48:47Z")

</div>

`Discourse.User.current().trust_level`

---

<div class="post-metadata">

**Author:** ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)\
**Post date:** [June 8, 2024, 12:45pm UTC](https://meta.discourse.org/t/how-to-find-out-if-user-is-logged-in-and-insert-some-js/43492/6 "2024-06-08T12:45:39Z")

</div>

Closed on June 8, 2024, 12:45pm UTC

This topic was automatically closed after 2961 days. New replies are no longer allowed.
