SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    check cookie problem

    Hello, i wrote this code to open a page if doesn't exist a cookie but it doesn't open the page do you know where I wrong?
    Thank you !

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head> 
    <title> HTML </title> 
    </head> 
    <body>
    <script type="text/javascript">
    
    function CookieLeggi(CookieNome)
    	{
    	if (CookieNome.length==0) return null;
    	var PosizioneIniziale = document.cookie.indexOf(CookieNome+"=");
    		if (PosizioneIniziale == -1) return null;
    		PosizioneIniziale += CookieNome.length+1;  
    	var PosizioneFinale = document.cookie.indexOf(";",PosizioneIniziale);
    		if (PosizioneFinale == -1) PosizioneFinale = document.cookie.length; 
    	return unescape(document.cookie.substring(PosizioneIniziale,PosizioneFinale));
    	}
    
    function CookieScrivi(name,value,expiresUdM,expires,path,domain,secure)
    	{
    	if (!name || !value) { return false }  
    	if ((expiresUdM && expires) && (expiresUdM!='GMT')) {
    		var ExpiresMillisec = ExpiresDate = Oggi = new Date();
    		switch (expiresUdM) {    // calcola i JS-millisecondi del momento di scadenza
    			case "anni":    ExpiresMillisec=Oggi.getTime()+expires*365*24*60*60*1000; break;
    			case "mesi":    ExpiresMillisec=Oggi.getTime()+expires*31*24*60*60*1000; break;
    			case "giorni":  ExpiresMillisec=Oggi.getTime()+expires*24*60*60*1000; break;
    			case "ore":     ExpiresMillisec=Oggi.getTime()+expires*60*60*1000; break;
    			case "minuti":  ExpiresMillisec=Oggi.getTime()+expires*60*1000; break;
    			case "secondi": ExpiresMillisec=Oggi.getTime()+expires*1000; break;
    			default:        ExpiresMillisec=Oggi.getTime()+expires; 
    			}
    		ExpiresDate.setTime(ExpiresMillisec);   
    		expires = ExpiresDate.toGMTString(); }  
    	secure = (secure=="1" || secure==1 || secure=="secure") ? 1 : "";
    	document.cookie = name + "=" +escape(value) +
    	        ( (expiresUdM && expires) ? "; expires=" + expires : "") +
    	        ( (path) ? "; path=" + path : "") + 
    	        ( (domain) ? "; domain=" + domain : "") +
    	        ( (secure) ? "; secure" : "");
    
    	if (CookieLeggi(name)==null && secure!=1) { return false; } else { return true; }
    	}
    </script>
    
    <script type="text/javascript">
    if (CookieLeggi(liveads) == "null")
    {
    window.open("http://www.google.com", "Google");
    CookieScrivi(liveads, adsm, ore, 12, /, .localhost, 1);
    }
    else
    {
    break;
    }
    </script>
    </body>
    </html>

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The if statement looks to be the issue, you have give a variable as the argument when it actually needs to be a string. The other issue is you have said to return null if the cookie doesn't exist but are checking for a string on return. See the below code that should work fine:

    Code JavaScript:
    if (CookieLeggi('liveads') == null) {
        // window.open stuff here
    }

    Also because your only checking for a null value you don't need an else statement which won't work anyway as break; can only be used within loops and switches.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •