简体   繁体   English

在每次打开主页时显示提示加载并存储值以问候用户

[英]Showing the Prompt on loading and store the value to greet the User every time he opens the HomePage

I'm trying to ask the Users "What's your name" using a prompt message to get its name. 我试图通过提示消息询问用户“您叫什么名字”。 Then, greeting the user with a welcome message based on the current time. 然后,根据当前时间向用户发送欢迎消息。 So far, I could show the message only by clicking a button. 到目前为止,我只能通过单击一个按钮来显示消息。 What I wanted to do is asking first for the name and, once got the Username, show the message directly when the HomePage is loaded. 我想做的是先询问名称,一旦获得用户名,则在加载HomePage时直接显示消息。

This is the HTML: 这是HTML:

  <form> <input id="show_button" class="button" type="button" value="Hey! Click Me! I know you!" onclick="greet(), salutoTempo()" /> </form> 

And this is the JavaScript Code: 这是JavaScript代码:

 var today = new Date(); var hourNow = today.getHours(); var saluto; if (hourNow > 17) { saluto = "Good evening" } else if (hourNow > 11) { saluto = "Good afternoon" } else if (hourNow > 0) { saluto = "Good morning" } else { saluto = "Welcome to Wolf the Barber!" } var el = document.getElementById("greeting"); function salutoTempo(){ document.getElementById("greeting").innerHTML = saluto + " "; } function greet(){ name = localStorage.getItem("name"); if (name == null || name == "null"){ alert("Hi, Stranger!"); name = prompt("What is your name?"); localStorage.setItem("name", name); var greeting = document.getElementById("greeting").innerHTML = (greeting); var Username = document.getElementById("greeting1").innerHTML = (name + "!"); } else { var greeting = document.getElementById("greeting").innerHTML = (greeting); var Username = document.getElementById("greeting1").innerHTML = (name + "!"); } // end greet } // end function 

Thanks guys! 多谢你们!

According to your question, you can use onload attribute in body tag. 根据您的问题,可以在body标签中使用onload属性。 Additionally, call the function salutoTempo . 另外,调用函数salutoTempo

<html>
<body onLoad = "greet()">
 <form> 
    <input id="show_button" class="button" type="button"   value="Hey! Click Me! I know you!" onclick="greet(), salutoTempo()" /> 
</form>

<span id="greeting"></span>
<br>
<span id="greeting1"></span>

<script>
var today = new Date();
var hourNow = today.getHours();   
var saluto;

if (hourNow > 17) {   
  saluto = "Good evening"
} else if (hourNow > 11) {
  saluto = "Good afternoon"
} else if (hourNow > 0) {
  saluto = "Good morning"
} else {
  saluto = "Welcome to Wolf the Barber!"
}

var el = document.getElementById("greeting");

function salutoTempo(){
    document.getElementById("greeting").innerHTML = saluto + " ";
}


function greet(){
   name = localStorage.getItem("name");

   if (name === null || name === "null"){
     alert("Hi, Stranger!");
     name = prompt("What is your name?");
     localStorage.setItem("name", name);     
     document.getElementById("greeting1").innerHTML = (name + "!");
   } else {
     document.getElementById("greeting1").innerHTML = (name + "!");
   } // end greet
   salutoTempo();
} // end function
</script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM