2016-03-25 37 views
-1

tanımlama bilgilerini, işlevlerini ve parametrelerini içerir neden aşağıdaki kod çalışmıyor? checkCookie()'u aramaya çalışıyorum ve bu işlev içinde bu orijinal işlevinin parametreleri aracılığıyla 2 diğer işlevi çağırın. Hiç işe yaramıyor.Neden bu çalışmıyor?

<!DOCTYPE html> 
<html> 
<head> 
<script> 

function setCookie(cname,cvalue,exdays) { 
    var d = new Date(); 
    d.setTime(d.getTime() + (exdays*24*60*60*1000)); 
    var expires = "expires=" + d.toGMTString(); 
    document.cookie = cname+"="+cvalue+"; "+expires; 
} 

function getCookie(cname) { 
    var name = cname + "="; 
    var ca = document.cookie.split(';'); 
    for(var i=0; i<ca.length; i++) { 
     var c = ca[i]; 
     while (c.charAt(0)==' ') c = c.substring(1); 
     if (c.indexOf(name) == 0) { 
      return c.substring(name.length, c.length); 
     } 
    } 
    return ""; 
} 

function checkCookie(cookieName, action1, action2) { 
    var temp = getCookie(cookieName); 
    if (temp!= "") { 
     window[action1]; 
    } else { 
     temp = window[action2]; 
     if (temp != "" && temp!= null) { 
      setCookie(cookieName, temp, 30); 
     } 
    } 
} 

function alertUser(message, varName){ 
    alert(message + varName); 
} 

function promptUser(message){ 
    prompt(message,""); 
} 

checkCookie("username", alertUser("Welcome back, ", temp), promptUser("Please enter in your name")); 
</script> 
</head> 
<body> 
</body> 
</html> 

Ben Sana denedim her şeyi göstermek olamayacağını özür dileriz yüzden izini kaybetmiş böylece birçok, birçok farklı kombinasyonları denedi. Yardımınız gerçekten takdir edilecek! :) Farklı eylemler gerektirebilecek birçok farklı çerez için birçok kez checkCookie'u aramak istiyorum, bu yüzden neden bu işlev içinde global işlevleri çağırmak için seçeneği açık bırakıyorum.

+1

altındaki yorumları okumak "çalışmıyor" yararlı bir tanım değil. Herhangi bir hatayla karşılaşıyor musunuz? JavaScript konsol penceresine yazılmış bir şey var mı? Hata ayıklayıcısını kullandığınızda ne olur? – Dai

+0

Hiçbir şey olmuyor, konsolda hiçbir hata olmuyor, sadece beyaz bir sayfa –

+0

Üzgünüz @Dai etiketlemeyi unuttum –

cevap

0

Birkaç sorunları var,

function setCookie(cname,cvalue,exdays) { 
    //omitted 
} 

function getCookie(cname) { 
    //omitted 
} 

function checkCookie(cookieName, action1, action2) { 
    //temp is available only here 
    var temp = getCookie(cookieName); 
    if (temp!= "") { 
     //action1(alertUser) bad usage 
     window[action1]; 
    } else { 
     //also action2(promptUser) bad usage 
     temp = window[action2]; 

     //redundant, just `if(temp)` 
     if (temp != "" && temp!= null) { 
      setCookie(cookieName, temp, 30); 
     } 
    } 
} 

function alertUser(message, varName){ 
    alert(message + varName); 
} 

//doesn't store value 
function promptUser(message){ 
    prompt(message,""); 
} 

//temp is not available here 
//although it's available, it's a bad idea to use vars from other functions 
//if person doesn't enter anything, this will break, no cycling 
checkCookie("username", alertUser("Welcome back, ", temp), promptUser("Please enter in your name")); 

iyi

function setCookie(cname, cvalue, exdays) { 
    //omitted 
} 

function getCookie(cname) { 
    //omitted 
} 

function checkCookie(cookieName, callback) { 
    var temp = getCookie(cookieName); 
    if (temp) { 
    callback(null, temp); 
    } else { 
    callback('No cookie', null); 
    } 
} 

function callbackFn(err, result) { 
    if (err) { 
    var uname = prompt('Please enter in your name'); 
    if (uname === '') { 
     //hit ok, but no value, re-show prompt 
     checkCookie('username', callbackFn); 
    } else if (uname) { 
     //entered value, hit ok 
     setCookie('username', uname, 30); 
    } else { 
     //hit cancel 
     checkCookie('username', callbackFn); 
     //or leave website? 
    } 
    } else { 
    alert('Welcome back, ' + result); 
    } 
} 
checkCookie('username', callbackFn);