简体   繁体   中英

How to show a javascript alert just one time

See, this is my code for the alert:

 function Alerta() { var person = prompt("¡Recuerda!:Todos Los Titulos Abren El Menu!\\nCual es tu nombre?", "Goku"); if (person != null) { document.getElementById("intro").innerHTML = "" + person + ""; } } 
 <html> <head> </head> <body onLoad="Alerta()" id="Inicio"> </body> 

The problem is that it shows up every time I enter in the page, refresh the page...or blink, so... I want it to do this: when you write a name and click "accept" it saves the name and the alert is never shown again, and when you click "cancel" it will show it every time you enter the page. Help please?

You could use localStorage to save the persons name between sessions, then only prompt if the person value is not set in localStorage . If you need backwards compatibility you can use cookies in place of localStorage

 function Alerta() { // get the value from localStorage var person = localStorage.person if (!person) { // person has not been set so ask the user person = prompt("¡Recuerda!:Todos Los Titulos Abren El Menu!\\nCual es tu nombre?", "Goku"); // set the person value in localStorage for next time localStorage.person = person } // set the value of your intro document.getElementById("intro").innerHTML = person; } 

当他们输入名称时,将其保存到可在页面上持久保存的名称(例如cookie或本地存储),如果已经设置,请在页面加载时取消提示。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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