最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How do I show a message box only the first time a page loads, using JavaScriptjQuery? - Stack Overflow

programmeradmin3浏览0评论

I want to show a message box, only the first time a webpage loads. If the user refreshes, it should no longer show up. The message should show up regardless of whether the user is already logged in or not.

Are cookies the only way to do this or there are any JavaScript library which can take of this? The technology stack is jQuery /JavaScript and PHP.

I want to show a message box, only the first time a webpage loads. If the user refreshes, it should no longer show up. The message should show up regardless of whether the user is already logged in or not.

Are cookies the only way to do this or there are any JavaScript library which can take of this? The technology stack is jQuery /JavaScript and PHP.

Share Improve this question asked Sep 11, 2013 at 20:12 GeocrafterGeocrafter 8151 gold badge8 silver badges18 bronze badges 2
  • you could use localStorage as well if you don't have to support older browsers – James Daly Commented Sep 11, 2013 at 20:15
  • 1 Interesting...not sure why I didn't think of that. Thanks. – Geocrafter Commented Sep 11, 2013 at 20:19
Add a ment  | 

4 Answers 4

Reset to default 3

Cookies is not the only way, you can use localStorage, Try this:

if(localStorage.getItem("firstTime")==null){
   alert("First Time Alert");
   localStorage.setItem("firstTime","done");
}

You should use your server side language(php) to store a session variable. Sessions store data across page loads.

If you have some way of preserving the state of the user (if they have visited that page or not), then you could pass that to the page and tell it to show the message while flagging the page as 'visited' on the server side (in the database, or wherever you are keeping that user/page data).

If you aren't maintaining that information anywhere, then you'll need to use cookies, or localStorage; however, this data can be lost (if the user clears it), and then the message will show again.

Here is a really good lightweight plugin that I use sometimes.

// plugin start //
    // First Time Visit Processing
    // copyright 10th January 2006, Stephen Chapman
    // permission to use this Javascript on your web page is granted
    // provided that all of the below code in this script (including this
    // ment) is used without any alteration
    function rC(nam) {var tC = document.cookie.split('; '); for (var i = tC.length - 1; i >= 0; i--) {var x = tC[i].split('='); if (nam == x[0]) return unescape(x[1]);} return '~';} function wC(nam,val) {document.cookie = nam + '=' + escape(val);} function lC(nam,pg) {var val = rC(nam); if (val.indexOf('~'+pg+'~') != -1) return false; val += pg + '~'; wC(nam,val); return true;} function firstTime(cN) {return lC('pWrD4jBo',cN);} function thisPage() {var page = location.href.substring(location.href.lastIndexOf('\/')+1); pos = page.indexOf('.');if (pos > -1) {page = page.substr(0,pos);} return page;}
// plugin finish //


// example code to call it - you may modify this as required
function start() {
   if (firstTime(thisPage())) {
      // this code only runs for first visit
      alert('wele');
   }
   // other code to run every time once page is loaded goes here
}
onload = start;

Example http://javascript.about./library/blfirst1.htm

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论