首页 文章

服务条款Javascript警报框

提问于
浏览
1

我正在使用Joomla 3.3.1构建一个站点,我想在用户第一次访问该站点时弹出一个警告框(而不是在后续页面点击或刷新页面时弹出) . 警告框应显示“通过访问此页面,您同意其服务条款”,“服务条款”是指向特定页面的链接 . 用户可以单击“确定” . 我是JavaScript的新手,但我尝试了下面的代码:

<script>
function TOS(){
alert("By visiting this page, you agree to its Terms of Service.");
}
TOS();
</script>

不出所料,只要我点击页面上的任何内容,就会弹出警报 . 我也尝试在onload中调用函数,但是我得到了类似的结果 .

您可以提供的任何指导或参考将非常感谢!

2 回答

  • 0

    就像是:

    var createCookie = function(name, value, days) {
        var expires;
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toGMTString();
        }
        else {
            expires = "";
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    }
    
    function getCookie(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) {
                    c_end = document.cookie.length;
                }
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    }
    function TOS(){
        var cookieName = 'hasVisitedBefore';
        var cookie = getCookie(cookieName);
        if (!cookie) {
            alert("By visiting this page, you agree to its Terms of Service.");
            createCookie(cookieName,true,3650);
        }
    }
    TOS();
    

    我在这个问题中使用了createCookie和getCookie函数:How do I create and read a value from cookie?

    这是一个工作小提琴:http://jsfiddle.net/kvLrt/1/

  • 0

    在模板的index.php文件中,在<body>标记下添加以下代码:

    <?php
        $alertCookie  = JFactory::getApplication()->input->cookie;
        $value        = $alertCookie->get('alertCookie', '');
        if (!$value){ ?>
    
        <script>
            function TOS(){
                alert("By visiting this page, you agree to its Terms of Service.");
            }
            TOS();
        </script>
        <?php 
        }
        else{
            $expireCookieUT = echo time() + 10000000;
            $alertCookie->set('alertCookie', '1', $expireCookieUT);
        }
    ?>
    

相关问题