【问题标题】:Terms of Service Javascript Alert Box服务条款 Javascript 警报框
【发布时间】:2014-07-27 04:04:55
【问题描述】:

我正在使用 Joomla 3.3.1 构建一个站点,并且我希望在用户首次访问该站点时弹出一个警告框(而不是在随后的页面点击或刷新页面时弹出)。警告框应显示“访问此页面,即表示您同意其服务条款”,其中“服务条款”是指向特定页面的链接。用户可以单击确定。我对 JavaScript 很陌生,但我尝试了以下代码:

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

不出所料,每当我点击页面上的任何内容时,都会弹出警报。我也尝试在 with onload 中调用该函数,但得到了类似的结果。

您能提供的任何指导或参考将不胜感激!

【问题讨论】:

    标签: javascript joomla


    【解决方案1】:

    类似:

    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/

    【讨论】:

    • 感谢您的帮助 - 我很感激 :)
    【解决方案2】:

    在模板的 index.php 文件中,在

    标签下添加以下代码:
    <?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);
        }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多