刷新

时间:2015-10-28 07:57:24

标签: javascript php wordpress popup

我正在为未登录的用户显示弹出窗口。我使用javascript和PHP执行此操作。

<?php

if ( ( is_single() || is_front_page() || is_page() ) 
       && !is_page('login') && !is_page('register') && !is_user_logged_in()){

    echo'<div class="overlay-bg">
</div>
<div class="overlay-content popup3">
    <h1>You must login or Register to view this site. do_shortcode("[sp_login_shortcode]");</h1>
</div>';
} 

?>

Javascript代码如下

$(document).ready(function(){

    // function to show our popups
    function showPopup(whichpopup){
        var docHeight = $(document).height(); //grab the height of the page
        var scrollTop = $(window).scrollTop(); //grab the px value from the top of the page to where you're scrolling
        $('.overlay-bg').show().css({'height' : docHeight}); //display your popup background and set height to the page height
        $('.popup'+whichpopup).show().css({'top': scrollTop+20+'px'}); //show the appropriate popup and set the content 20px from the window top
    }

    // function to close our popups
    function closePopup(){
        $('.overlay-bg, .overlay-content').hide(); //hide the overlay
    }

    // timer if we want to show a popup after a few seconds.
    //get rid of this if you don't want a popup to show up automatically
    setTimeout(function() {
        // Show popup3 after 2 seconds
        showPopup(3);
    }, 4000);


});

我希望在访问该网站后第一次在访问者的5分钟后显示弹出窗口。但是当相同的访问者刷新页面或将来再次出现时,我希望在5分钟后立即显示弹出窗口。

如何在上面的代码中实现这一点。请帮忙。

由于

2 个答案:

答案 0 :(得分:0)

这里有关于如何实现这一目标的想法。

  1. 用户打开页面
  2. 检查是否已设置Cookie is_first_time

    2a上。如果未设置,则在5分钟后显示弹出窗口并设置is_first_time cookie 2B。如果已设置,则立即显示弹出窗口

答案 1 :(得分:0)

在朋友的帮助下,我从自己那里得到了答案。曲奇饼应该设置如下

<?php
setcookie("visited",true);

if(!empty($_COOKIE['visited']) && $_COOKIE['visited'] == true)
 $popup_time = 0;
 else
 $popup_time = 60000; 
?>

这里$ popup_time变量设置为函数javascript代码,如下所示

setTimeout(function() {
        // Show popup3 after 2 seconds
        showPopup(3);
    }, <?php echo $popup_time?>);

那就是它:)。我很沮丧,没有人在这里给我一个正确的方法。

无论如何,谢谢

相关问题