3

I have a website created using laravel. I want the user to be able to see a popup the first time he comes to my site.

By first time I mean- the user comes to any page on my site(this is first time), visits some of the linked pages on site(now, these page visits are not first time), opens facebook in the same tab(thus leaving my site) and then again opens my site in the same tab(this is first time again).

The basic reason behind one time popping up is not to bother the user again and again while he is navigating the site. But, once he leaves and comes back again, then I want to treat it like a new visit(and show the pop up again).

I know I can do popup thing with setTimeout() in jQuery. But how to do it just once? Please note that, the pop can appear on any page(if its first page of the current visit), not only on the home page.

halkujabra
  • 2,844
  • 3
  • 25
  • 35

4 Answers4

2

Use a plugin like jQuery Cookie, then simply do:

if(!$.cookie("popup"))
{
 your_popup_function();
 $.cookie("popup",true);
}

Edit: since you edited your question regarding your definition of "first time visit", I'd advise the approach Walter Brand suggested, using the referrer. Taken from this post:

document.referrer.indexOf(location.protocol + "//" + location.host) === 0;
Community
  • 1
  • 1
René Roth
  • 1,979
  • 1
  • 18
  • 25
  • 1
    I guess the problem with using cookies is that the session cookies is still available when you navigate to another site and then come back in the same tab. – Walter Brand Mar 31 '14 at 06:59
2

you could use document.referrer, and check if the previous page is one of your domain. But not all visitors/browsers will return this value for security reasons.

So this is unreliable.

Walter Brand
  • 679
  • 3
  • 9
2

Solution without using plugins:

var adCookie = document.cookie.indexOf('ad_displayed')

if (adCookie != -1) {
   your_popup_function();
   document.cookie = "ad_displayed=1";
}

This is the idea, of course you can set expiry date on the cookie. Read more here

  • 1
    You won't be able to know when a visitor leaves your site and later come back in the same tab with a cookie. So in this case this answer is not useful. – Walter Brand Mar 31 '14 at 07:18
  • This cookie solution is preferrable to mine if you want maximum performance and minimum code size. – René Roth Mar 31 '14 at 08:46
1

Hi i did this solution while solving the client requirement "to show pop-up only once throughout navigating to different pages of the site" and it works good for me. I did it using cookie and setting the cookie value to the cookie creation time than i make the difference of the cookie value with current time using javascript setInterval function and compare the difference with the time on which i want to show the pop-up and it's work.

$(document).ready(function() 
{
var myVar=""; 

 function getCookie(name)
{
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}

function callagain()
          { 
            if(!getCookie("magazine"))
             {
              var c1= new Date();
              var c2=c1.getTime();
              document.cookie="magazine="+c2;
              }   
            var cvalue= getCookie("magazine");
            var cvalue2=parseInt(cvalue);
            myVar=setInterval(function(){ call22(cvalue2);},1000);     
       }

function call22(abcd)
{
var curdate = new Date();
var curtime = curdate.getTime();
var curtime2=parseInt(curtime);
var result=curtime2 - abcd;
if( (result >30000) && (result < 31000) )
             {
               alert(“POP UP  ONCE THROUGHOUT THE SITE”);
                clearInterval(myVar);
             }
}
callagain();                 
});