Running javascript after x number of page views

I want to show an advertising pop-up after users have been on my site either for a certain amount of time, or after a certain number of page views. For instance. I want it to appear after the user has viewed 3 pages.

How to do this using JavaScript / jQuery or PHP?

+3
source share
5 answers

Ok, people have suggested a PHP solution, I will complement javascript. Here is a very simple approach localStorage:

if ((localStorage.pageViews = (+localStorage.pageViews || 0) + 1) > 3) {
    alert('Marketing');
}

Demo: http://jsfiddle.net/vBLv5/ (refresh page 3 times).

+6
source

PHP javascript, x ( cookie/)

Javascript + cookie localstorage ...

+4

$_SESSION PHP . , .

if(!isset($_SESSION['page_runs']))
{
    $_SESSION['page_runs'] = 1;
}else{
    $_SESSION['page_runs'] = $_SESSION['page_runs'] + 1;

    if($_SESSION['page_runs'] == 3)
    {
        echo '<script></script>';
    }
}
+1
<?php
   session_start();
   if( isset( $_SESSION['counter'] ) )
   {
      $_SESSION['counter'] += 1;
   }
   else
   {
      $_SESSION['counter'] = 1;
   }
   $msg = "You have visited this page ".  $_SESSION['counter'];
   $msg .= "in this session.";
?>
<html>
<head>
<title>Setting up a PHP session</title>
</head>
<body>
<?php  echo ( $msg ); ?>
</body>
</html>
+1

Cookie, = 1

$_COOKIE['visit_count'] = 1;

,

$_COOKIE['visit_count'] = $_COOKIE['visit_count'] + 1; 

, .

if($_COOKIE['visit_count'] === N) {
    //Do some stuff
}

cookie Javascript. , Cookie httponly.

0

All Articles