2014-07-19 49 views
1

我正在建設一個使用Joomla 3.3.1的網站,並且我希望每當用戶第一次訪問該網站時彈出一個警告框(並且不會在隨後的頁面點擊或刷新時彈出這一頁)。警報框應該顯示「通過訪問此頁面,您同意其服務條款」,而「服務條款」是指向特定頁面的鏈接。用戶可以點擊確定。我很JavaScript新手,但我想下面的代碼:服務條款Javascript Alert Box

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

不出所料,本作的警報彈出任何時候我在任何頁面上點擊。我也試着用onload調用函數,但是我得到了類似的結果。

任何您可以提供的指導或參考將非常感謝!

回答

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

謝謝你的幫助 - 我欣賞它:) – user3856284

0

在模板中的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); 
    } 
?> 
+0

非常感謝! – user3856284