Don't Laugh at Me: How to Add JS to Widget to Display Text

Technical Support
  • Please keep all snickering to a quiet din!

    How would I go about creating a script to display text in a NodeBB widget based on the value of a variable? For example when I try the following I get an error that document.write is not a function.

    <script type="text/javascript">
    {
        if(app.user.username === me) {
    		document.write('Display This Information');
        }
    }
    </script>
    

    Thank you.

  • You want something like this

    <script type="text/javascript">
        if(app.user.username === 'me') {
             $('#someElement').html('some content')
        }
    </script>
    
  • @baris Thank you. It is working great.

  • I have to say that programmatically being able to show and hide content in a widget is fabulous. I now have a Global > Global Header widget that displays an image and text only when it is the "home" page.

    This is how I implemented one widget:

    <widgetGH1></widgetGH1>
    <script type="text/javascript">
        if(location.href === 'https://example.com/') {
             $('widgetGH1').html('<img src="//example.com/uploads/image.png">  Site Welcome Text');
        }
    </script>
    


Suggested Topics


| | | |