简体   繁体   English

如何在页面第一次加载时执行Javascript警报

[英]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 : 您可以使用localStoragecookies

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 in JS JS中的Cookie

看到此链接,您将获得一些基于cookie的想法示例 。一旦键入您的值,然后刷新它,它将显示该值。

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

相关问题 如何仅在第一次使用 javascript 滚动页面时发出警报? - How to alert when scroll page only first time using javascript? 如何在第一次加载页面时使用JavaScript / jQuery显示消息框? - How do I show a message box only the first time a page loads, using JavaScript / jQuery? 首次加载页面时,javascript为ipad设置绝对位置 - javascript setting absolute position for ipad when the page loads the first time 首次加载页面时,然后在时间段之后执行jQuery代码 - Execute jQuery code when page loads for first time and then after time period 仅第一页在phonegap中加载javascript - Only first page loads javascript in phonegap 侧栏显示页面首次加载的时间 - sidebar showing when the page loads for the first time javascript警报阻止页面加载 - javascript alert block page loads 如何在页面加载时停止javascript中的警报触发,并在单击按钮时使其运行? - How can I stop an alert in javascript from firing when the page loads, and make it run when a button is clicked? 仅在首次加载页面时在第一张幻灯片上添加课程 - Add class only on first slide the first time the page loads 如何从后台代码执行javascript函数,但仅在页面加载后执行 - How to execute javascript function from code-behind but only after the page loads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM