-2

I have a bootstrap/jquery/javascript app that allows the person to log in then they are taken to a table of contents page where they can click a link in order to play a video. I have code on the table of contents page that gives them a personalized message.

<script type="text/javascript" language="javascript">
        function WelcomeMsg() {
            // Retrieve the users name.
          var name = localStorage.getItem('name');

          if (name != "undefined" && name != "null") {
            document.getElementById('welcomeMessage').innerHTML = "Welcome " + name + " !" + "&nbsp";
          } else
            document.getElementById('welcomeMessage').innerHTML = "Hello!";
          }
    </script>

The code works well when someone logs in properly but if someone doesn't log in and skips directly to the tableOfContents.html page then I want the message to say simply "Hello." Unfortunately right now in that situation it says "Welcome null." Can someone tell me what I'm doing wrong? Thank you.

John M.
  • 347
  • 1
  • 11
  • 1
    [How to check for an undefined or null variable in JavaScript?](http://stackoverflow.com/questions/2559318/how-to-check-for-an-undefined-or-null-variable-in-javascript) – Rick S Dec 21 '16 at 20:11

1 Answers1

0

Change

name != "undefined" && name != "null"

to

name != undefined && name != null
S Chisefu
  • 36
  • 4