Adding a personalized welcome message

Next we will add another bit of code, to change the page‘s title to include a personalized welcome message when the user first navigates to the site. This welcome message will persist when the user leaves the site and then comes back. We will also include an option to change the user and therefore the welcome message anytime it is required.

  1. In index.html, add the following line just before the <script> element:

    <button>Change user</button>
    

      

  2. In main.js, add the following code at the bottom of the file, exactly as written — this grabs references to the new button we added and the heading, and stores them in variables:

    var myButton = document.querySelector(‘button‘);
    var myHeading = document.querySelector(‘h1‘);
    

      

  3. Now add the following function to set the personalized greeting — this won‘t do anything yet, but we‘ll use it later on:

    function setUserName() {
      var myName = prompt(‘Please enter your name.‘);
      localStorage.setItem(‘name‘, myName);
      myHeading.innerHTML = ‘Mozilla is cool, ‘ + myName;
    }
    

      

    This function contains a prompt() function, which brings up a dialog box, a bit likealert() except that prompt() asks the user to enter some data, and stores that data in a variable after the user presses OK. In this case, we are asking the user to enter their name. Next, we call on an API called localStorage, which allows us to store data in the browser and retrieve it later on. We use localStorage‘s setItem()function to create and store a data item called ‘name‘, and set its value to themyName variable that contains the name the user entered. Finally, we set theinnerHTML of the heading to a string, plus the user‘s name.

  4. Next, add this if ... else block — we could call this the initialization code, as it sets up the app when it first loads:

    if(!localStorage.getItem(‘name‘)) {
      setUserName();
    } else {
      var storedName = localStorage.getItem(‘name‘);
      myHeading.innerHTML = ‘Mozilla is cool, ‘ + storedName;
    }
    

      

    This block first uses the negation operator (logical NOT) to check whether the namedata item exists. If not, the setUserName() function is run to create it. If so (i.e. the user set it during a previous visit), we retrieve the stored name using getItem() and set the innerHTML of the heading to a string, plus the user‘s name, the same as we did inside setUserName().

  5. Finally, put the below onclick event handler on the button, so that when clicked thesetUserName() function is run. This allows the user to set a new name whenever they want by pressing the button:

    myButton.onclick = function() {
      setUserName();
    }
    

      

Now when you first visit the site, it‘ll ask you for your username then give you a personalized message. You can then change the name any time you like by pressing the button. As an added bonus, because the name is stored inside localStorage, it persists after the site is closed down, so the personalized message will still be there when you open the site up again!

时间: 2024-08-24 12:08:57

Adding a personalized welcome message的相关文章

Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

catalog 1. How to Add New Functions to MySQL 2. Features of the User-Defined Function Interface 3. User-Defined Function 4. UDF Argument Processing 5. UDF Return Values and Error Handling 6. UDF Compiling and Installing 7. Adding a New Native Functio

Android Message Handling Mechanism

转自:http://solarex.github.io/blog/2015/09/22/android-message-handling-mechanism/ Android is a message driven, message driven several elements: The message says: Message Message queue: MessageQueue The news cycle, remove the message processing for circ

紫书第一章训练1 D -Message Decoding(UVA213) by 16黄睿博

来源:http://m.blog.csdn.net/article/details?id=70766751 Some message encoding schemes require that an encoded message be sent in two parts. The ?rst part, called the header, contains the characters of the message. The second part contains a pattern tha

Adding Action Buttons

为简单起见,此处只保留了关键代码.详细过程参见官方教程:Adding Action Buttons 在AndroidManifest.xml文件中,我们有如下代码: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example

System and method for assigning a message

A processor of a plurality of processors includes a processor core and a message?manager. The message?manager?is in communication with the processor core. The message?manager?to receive a message from a second processor of the plurality of processors

UVa 213 Message Decoding(World Finals1991,字符串)

 Message Decoding  Some message encoding schemes require that an encoded message be sent in two parts. The first part, called the header, contains the characters of the message. The second part contains a pattern that represents the message. You must

Message Decoding

Some message encoding schemes require that an encoded message be sent in two parts. The first part, called the header, contains the characters of the message. The second part contains a pattern that represents the message. You must write a program th

spring的RabbitTemplate 接收Message源码导读

1,首先调用类SimpleMessageListenerContainer的内部类AsyncMessageProcessingConsumer的run方法.内部类的主要属性如下 private final BlockingQueueConsumer consumer; private final CountDownLatch start; private volatile FatalListenerStartupException startupException; 2,内部类的run方法如下

You Probably Don’t Need a Message Queue

原文地址 I’m a minimalist, and I don’t like to complicate software too early and unnecessarily. And adding components to a software system is one of the things that adds a significant amount of complexity. So let’s talk about message queues. Message Queu