1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

need help with javascript cookies.

Discussion in 'JavaScript' started by FR3@K, Oct 4, 2007.

  1. #1
    hello everyone.
    if you can help me , then please help !
    I am trying to set a cookie using onclick on href tag.
    and i want to retrieve it when the page is loaded.
    it should be set like this :

    <a id="switchwidthwide" href="javascript:void(0)" onclick='setcookies("name of cookie", "value of cookie")' title="Wide width"></a>
    Code (markup):

    i want it to be retrived in body tag like this :

    <body id="page" if cookie value = normal , echo class="cookie value" else echo class="font-small width-fluid">
    Code (markup):
    can someone please explain how can i do this?
    i am new at javascript and php.
    and someone told me that this thing will be done with javascript.
     
    FR3@K, Oct 4, 2007 IP
  2. temp2

    temp2 Well-Known Member

    Messages:
    1,228
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #2
    <body id="page" <?php if ($_COOKIE['cookie value'] == 'normal') echo 'class="cookie value"'; else echo 'class="font-small width-fluid"';?>>
    Code (markup):
     
    temp2, Oct 4, 2007 IP
  3. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    er.. dont you think you are mixing up javascript and php?
    you didnt tell me how to set the cookies using onclick.
     
    FR3@K, Oct 4, 2007 IP
  4. joesgraphics

    joesgraphics Peon

    Messages:
    206
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well the reason i think he got mixed up is since u used as Ur example echo etc that aint used in js but is in php.

    To do what u are wanting in js you will need some functions to get, add cookies there are a few unready made js functions out there like here.
    code to use with those functions would be:

    <script type="text/javascript">
    function addCookie(name, value) {
    
    createCookie(name,value, 360);
    }
    
    if (!readCookie("body-size")) {
    createCookie('body-size', 'width-fluid', 360);
    document.getElementById('switchwidthwide').className = readCookie("body-size");
    }
    else {
    document.getElementById('switchwidthwide').className = readCookie("body-size");
    }
    
    </script>
    
    then on the onclick use onclick="addCookie('body-size', 'width-small')"
    
    PHP:

    Or you could use mootools and use this code below for your example:

    
    <script type="text/javascript" src="mootools.js"></script>
    <script type="text/javascript">
    function addCookie(name, value) {
    Cookie.set(name, value, {duration: 360});
    }
    
    if (!Cookie.get("body-size")) {
    Cookie.set('body-size', 'width-fluid', {duration: 360});
    document.getElementById('switchwidthwide').className = Cookie.get("body-size");
    }
    else {
    document.getElementById('switchwidthwide').className = Cookie.get("body-size");
    }
    
    </script>
    
    then on the onclick use onclick="addCookie('body-size', 'width-small')"
    
    PHP:

    It should work dont think i did anythink wrong.

    Joe
     
    joesgraphics, Oct 4, 2007 IP
  5. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks joesgraphics , +rep for you, your code really works :D
     
    FR3@K, Oct 5, 2007 IP
  6. joesgraphics

    joesgraphics Peon

    Messages:
    206
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Glad it works, thanks for the rep

    Joe
     
    joesgraphics, Oct 8, 2007 IP