www.webdeveloper.com
Results 1 to 2 of 2

Thread: cookie multiple langauges

  1. #1
    Join Date
    Aug 2012
    Posts
    1

    Talking cookie multiple langauges

    hello there,

    i would like to make a cookie from the homepage where if you click on a html link, the next time you come back on the homepage
    you will be automaticly be redirected to that link you choosed.

    here is my code i have so far

    <!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>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>cookie_naam.html</title>

    <script type="text/javascript">

    function getCookie(NameOfCookie){



    /* is er een cookie? */



    if (document.cookie.length > 0) {



    alert("de cookie " + document.cookie);

    /* op een pagina kunnen er meerder cookies staan, is het deze cookie die we willen?*/

    /* we zoeken of NameOfCookie= voorkomt in de string */



    begin = document.cookie.indexOf(NameOfCookie+"=");



    /* als we de cookie vinden dan moet de index helemaal vooraan zijn dus 0*/

    alert(" de index "+ begin);



    if (begin != -1){



    /* begin krijgt nu de positie na het = teken */

    begin += NameOfCookie.length+1;



    /* zoekt de ; vanaf begin dat is dan het einde van de waarde voor naam */

    end = document.cookie.indexOf(";", begin);





    alert("het begin "+ begin +" en het einde "+end);

    /* als er geen ; staat dan komt na die waarde niets meer dus is het einde de laatste positie van die waarde */



    if (end == -1) end = document.cookie.length;

    return unescape(document.cookie.substring(begin, end)); }

    }

    return null;

    }



    function setCookie(NameOfCookie, value, expiredays){



    var ExpireDate = new Date ();



    /* een cookie blijft een bepaalde tijd, de tijd wordt wordt berekend, huidige tijd wordt genomen en aantal dagen omgezet in millisec wordt erbij opgeteld*/



    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));



    document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());

    }



    function delCookie (NameOfCookie){

    if (getCookie(NameOfCookie)) {

    document.cookie = NameOfCookie + "=" + "; expires= Fri, 11 Aug 2007 00:00:01 GMT";

    }

    }



    function LinkCookie(){

    username=getCookie('username');

    if (username!=null) {

    alert('Hi nice to meet you please take a link, when you click on this link the next time you open this site you will be automaticly redirected to your choiche')

    }

    else {

    ###### username=(); ##########

    setCookie('username',username,365)

    }

    }



    </script>

    </head>

    <body onload="LinkCookie()">
    <a href="www.google.com" onclick='(getCookie("LinkCookie"));return false'>Get Cookie!</a>
    <a href="www.yahoo.com" onclick='(getCookie("LinkCookie"));return false'>Get Cookie!</a>



    </body>

    </html>

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by afrospacechrist View Post
    hello there,

    i would like to make a cookie from the homepage where if you click on a html link, the next time you come back on the homepage
    you will be automaticly be redirected to that link you choosed.
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Test Page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type='text/css'>
    a,a:link,a:visited{padding:0.2em;}
    a:hover,a:active{padding:0.2em;text-decoration:none;background-color:#00d;color:#fff}
    </style>
    </head>
    <body>
    
    <a href='http://google.co.uk'>Google</a><a href='http://ebay.co.uk'>EBay</a><a href='http://youtube.co.uk'><span>YouTube</span></a>
    
    
    <script type='text/javascript'>
    
    (function( cName, days )
    {
      var cValue;  
        
      function addHandler( obj, evt, funcRef )
      {
        if( obj.attachEvent )
          obj.attachEvent( 'on' + evt, funcRef );
        else     
          obj.addEventListener( evt, funcRef, false ); 
      }
        
      addHandler( document, 'click', function( e )
      {
        var srcElem = e.srcElement || e.target,
            dt = new Date(/*2863294C6F67696320416C69*/);
        
        while( srcElem && srcElem.nodeName !== 'A' )
          srcElem = srcElem.parentNode;
          
        if( srcElem )
        {
          dt.setDate( dt.getDate() + days );  
          document.cookie = cName + '=' + encodeURIComponent( srcElem.href ) + ";expires=" + dt.toUTCString();      
        }  
      });
      
      if( typeof document.cookie === 'string' && ( cValue = document.cookie.match( "(^|\s*)" + cName + "=" + "([^\s;]+)" ) ) )
      {
        cValue = decodeURIComponent( cValue[ 2 ] );
        
        if( confirm( 'Visit last link clicked ?' + '\n\n' + cValue ) )
          location.href = cValue;
        else
          document.cookie = cName + "=0;expires=" + new Date().toUTCString(); 
      }
        
    })( 'lastLink', 30 );
    
    </script>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles