Home > Web Front-end > Front-end Q&A > How to use JavaScript to implement greeting function

How to use JavaScript to implement greeting function

PHPz
Release: 2023-04-24 15:17:51
Original
1278 people have browsed it

In our daily life, greetings are a very common way of communication. Whether meeting friends or communicating with colleagues, people want to show courtesy and enthusiasm through greetings. The same is true for online communication. As developers, we can implement the greeting function through JavaScript to make the website or application more friendly and friendly.

1. Basic greetings

First, we need to define some basic greetings. Here, we can define three different greetings:

  • Good morning
  • Good afternoon
  • Good evening

We can output the corresponding greeting by getting the current time and judging it. The code is as follows:

var date = new Date();
var hour = date.getHours();

if(hour >= 5 && hour < 12){
    document.write("早上好");
}
else if(hour >= 12 && hour < 18){
    document.write("下午好");
}
else {
    document.write("晚上好");
}
Copy after login

This code first obtains the current time object through new Date(), and then uses the getHours() method to obtain the current number of hours. Next, make a judgment in the if-else statement. If the current time is in the morning (between 5 o'clock and 12 o'clock), then output "Good morning". If it is at noon (between 12 o'clock and 18 o'clock), then output " Good afternoon", otherwise output "Good evening".

2. Output greetings based on the name entered by the user

In addition to basic greetings, we can also make our greetings more personalized, such as outputting greetings based on the names entered by the user. The code is as follows:

var name = prompt("请输入您的名字:");
var date = new Date();
var hour = date.getHours();
var greet;

if(hour >= 5 && hour < 12){
    greet = "早上好";
}
else if(hour >= 12 && hour < 18){
    greet = "下午好";
}
else {
    greet = "晚上好";
}

document.write("<p>"+greet+","+name+",欢迎来到我们的网站!</p>");
Copy after login

This code first uses the prompt() method to pop up an input box and ask the user to enter their name. Next, the judgment is made based on the obtained current hour and the previous code segment. The difference is that the greeting is stored in a variable in the judgment statement. Finally, the greeting and the name entered by the user are output to the web page.

3. Implement random greetings

In the first two examples, we use predefined greetings for output. Sometimes we may need more interesting and vivid greetings. language. At this time, we can create an array containing multiple greetings and then randomly select one for output. The code is as follows:

var greetings = ["您好!", "欢迎光临!", "祝您今天有一个愉快的一天!", "早上好!", "下午好!", "晚上好!", "最近好吗?"];
var randomGreet = greetings[Math.floor(Math.random()*greetings.length)];
document.write("<p>"+randomGreet+"</p>");
Copy after login

This code first defines an array containing multiple greetingsgreetings, and then uses Math.random() and The Math.floor() method randomly selects a greeting and outputs it to the web page.

Summary:

The greeting function implemented through JavaScript can be implemented through predefined greetings, user-entered names, and random selection. When developing a website or application, the greeting feature can make it easier for users to feel friendly and friendly, making the experience more comfortable.

The above is the detailed content of How to use JavaScript to implement greeting function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template