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?
Ok, people have suggested a PHP solution, I will complement javascript. Here is a very simple approach localStorage:
localStorage
if ((localStorage.pageViews = (+localStorage.pageViews || 0) + 1) > 3) { alert('Marketing'); }
Demo: http://jsfiddle.net/vBLv5/ (refresh page 3 times).
PHP javascript, x ( cookie/)
Javascript + cookie localstorage ...
$_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>'; } }
<?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>
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.