1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to automatically open a new tab when visitors access website

Discussion in 'General Programming Chat' started by Mr Grey, May 4, 2014.

  1. Mr Grey

    Mr Grey Newbie

    Joined:
    Jul 15, 2013
    Messages:
    28
    Likes Received:
    2
    Hi there,

    I'd like to know if one of you guys could take some time to explain to me how to automatically open a new tab when visitors access my website for the first time (without them clicking any link).

    This should only happen on the first visited page (on every page it would be too much of a pain in the ass for the readers).

    Any idea how I could do that ? So far I've been thinking about something like that:

    Code:
    $("#afflink").click(function() {
           setTimeout(function() {
                window.open(
                   'url',   '_blank' 
            );
         
          }, 2000); // 2000 for 2 seconds, 1000 for 1 second etc
    });
    
    Problem is this will be active all the time not only on the first visited page so far too agressive for users.

    Thanks for your help !
     
  2. edge241

    edge241 Registered Member

    Joined:
    Oct 31, 2013
    Messages:
    85
    Likes Received:
    11
    You'll probably have to set some kind of cookie to recognise if they've visited the site before. That way if they have no cookie then open the tab. If they have a cookie, dont open the tab. I'm not that tech savy with java script so I'm not 100% how to do it. But search around for that? I'm sure it'd be easy to implement
     
  3. webhostingproviders

    webhostingproviders Jr. VIP Jr. VIP Premium Member

    Joined:
    Aug 8, 2013
    Messages:
    848
    Likes Received:
    265
    Occupation:
    Internet Marketer
    Location:
    Planet Earth
    Home Page:
    hmm it can be handled through CSS - I dont know much in depth though
     
  4. devtheworld

    devtheworld BANNED BANNED

    Joined:
    Mar 21, 2014
    Messages:
    123
    Likes Received:
    12
    Css is just for styles, it is not for functions, only cookies, and js can able to help.
     
  5. DaQuark

    DaQuark Junior Member

    Joined:
    Jun 4, 2011
    Messages:
    130
    Likes Received:
    43
    Location:
    home sweet home
    I found a simple JS-script that sets a cookie, when someone is new. Maybe just increase the expire time. Hope it helps, Cheers

    Code:
    if (document.cookie.indexOf("visited") >= 0) {
      // a returning customer
      // do nothing or do something else :-)
    }
    else {
      // set new cookie
      expiry = new Date();
      expiry.setTime(date.getTime()+(10*60*1000)); // Ten minutes
    
    
      // Date()'s toGMTSting() method will format the date correctly for a cookie
      document.cookie = "visited=yes; expires=" + expiry.toGMTString();
      
      // your open link function here
    }
    
     
    • Thanks Thanks x 1
  6. Mr Grey

    Mr Grey Newbie

    Joined:
    Jul 15, 2013
    Messages:
    28
    Likes Received:
    2
    Thanks guys ! I'll try to figure something out based on your info. Hope this will work :D