2013-01-24 76 views
1

我只想給第一次用戶一個我的網站的「旅遊」。我想用JavaScript做。我的意思是,如果用戶從未見過我的網站,並且這是他/她的第一次訪問,他們將獲得關於如何使用該網站的教程。如果他們再次訪問或重新加載頁面,他們不應該看到工具提示。這可以通過JavaScript Cookies來完成嗎?我找到了一個PHP代碼,但現在我想避免使用PHP。如何只顯示第一次訪問者的特定工具提示?

<?php 
// Top of the page, before sending out ANY output to the page. 
    $user_is_first_timer = !isset($_COOKIE["FirstTimer"]); 

// Set the cookie so that the message doesn't show again 
    setcookie("FirstTimer", 1, strtotime('+1 year')); 
?> 




<H1>hi!</h1><br> 


<!-- Put this anywhere on your page. --> 
<?php if($user_is_first_timer): ?> 
    Hello there! you're a first time user!. 
<?php endif; ?> 

如果不能用JS完成,可以用.htaccess來完成嗎?我也發現了這一點:

RewriteEngine on 
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC] 
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC] 
+0

如果cookie不存在,做你的提示東西。否則,不要。 – MikeSmithDev

+0

我對Cookies沒有任何經驗,我無法在網上找到與我的主題相關的任何內容。 – DaKoder

回答

4

當然,雖然我寧願用localStorage存儲「已經訪問過的用戶」信息,因爲如果你設置一個cookie,然後該cookie都會在每一個被髮送到服務器。單。請求。製作。至。你的。服務器。

所以,試試這個:

if(!window.localStorage) { 
    // no localStorage (old browser) - either fall back to cookie method, 
    // or just do nothing and skip the whole tour thing - if the user 
    // can't be bothered to upgrade, why should you bother to accomodate them? 
} 
else { 
    if(!window.localStorage.isReturningVisitor) { 
     // do all the tour stuff here 
     window.localStorage.isReturningVisitor = true; 
    } 
} 
+0

是的! localStorage的!我比Cookies更喜歡它。我不知道這是可能的。我現在就測試一下。 – DaKoder

+0

如果你打算按照「什麼都不做」的建議,我建議'if(window.localStorage &&!window.localStorage.isReturningVisitor){/ * do tour * /}' –

+1

你可以創建一個JSFiddle嗎? – DaKoder

相關問題