[英]How to execute Javascript alert when page loads the first time only
I am trying to execute a javascript alert, but only alert if it is the first time that browser / computer has viewed that page - or something similar to that. 我正在尝试执行JavaScript警报,但是仅当浏览器/计算机第一次查看该页面或类似内容时才发出警报。
How would this be done? 怎么做? I have written the Javascript to what I think it be similar to.
我已经按照自己的想法写了Javascript。
function alert() {
alert(" Please view this is Firefox");
}
if (first time viewing this page) {
alert();
}
I really appreciate your help 我非常感谢你的帮助
You could use the JQuery Cookie Plugin for this. 您可以为此使用JQuery Cookie插件 。
function showPopUp() {
var cookie = $.cookie('the_cookie');
if(!cookie){
alert(" Please view this in Firefox");
$.cookie('the_cookie', 'the_value');
}
}
showPopUp();
You can use localStorage
or cookies
: 您可以使用
localStorage
或cookies
:
Here is an example with localStorage
: 以下是
localStorage
的示例:
var visited = localStorage.getItem('visited');
if (!visited) {
alert("Please view this is Firefox");
localStorage.setItem('visited', true);
}
Don't use a Cookie
it will be sent to the server at each time your make request. 不要使用
Cookie
,每次发出请求时都会将其发送到服务器。 You can use Local Storage
instead, like: 您可以使用
Local Storage
,例如:
function load() {
var isFired = localStorage.getItem('checkFired');
if (isFired != '1'){
alert(" Please view this is Firefox");
localStorage.setItem('checkFired', '1');
}
}
load();
Set cookie 设置cookie
document.cookie="first=first";
Read a Cookie with JavaScript 使用JavaScript读取Cookie
var x = document.cookie;
Example: 例:
function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
{
var c = ca[i].trim();
if (c.indexOf(name)==0) return c.substring(name.length,c.length);
}
return "";
}
function checkCookie()
{
var first=getCookie("first");
if(first == "first")
{
alert(" Please view this is Firefox");
}else{
document.cookie="first=first";
}
}
看到此链接,您将获得一些基于cookie的想法示例 。一旦键入您的值,然后刷新它,它将显示该值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.