Hello,

I have a problem with setting a cookie.
People can hide or show background but when they go to another page of the website this needs to be remembered so I need to save it in a cookie.
My code so far is

<!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>Menu toggle background</title>

    <style type="text/css">
    <!--
        * { margin: 0; padding: 0; }

        html, body { margin: 0; padding: 0; width: 100%; height: 100%; } 
        img { border: 0; } 

        img#back { 
            position: fixed; 
            left: 0px; 
            top: 0px; 
            width: 100%; 
            height: 100%; 
            z-index: -1; 
            _display:none;
        }   


        body {
            background-color: #94a5c3;
            font: 14px Century Gothic, Arial, Helvetica;
            color: #000080; /*#010271;*/
        }
        //-->
        </style>


<!-- SET COOKIE -->
<script type = "text/javascript">
// <body onload ="readCookie('mycookiename'); eraseCookie('mycookiename'); createCookie('mycookiename','anything', 365)">
//name = "mycookiename" or whatever name you wish to give the cookie
//value = "anything" - in this case the url of the image
//days = number of days cookie to remain active (say 365)

//Using the following functions are from quirksmode. - START
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    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, c.length);
        if (c.indexOf(nameEQ) == 0) 
            return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function toggle() {
eraseCookie('background');
    var element = document.getElementById("back");
    var text = document.getElementById("displayText");
        if(element.style.display == "none") 
        {
            element.style.display = "block";
            text.innerHTML = "Hide Background";
            createCookie( "background", "id", 365);
        }
        else 
        {
        element.style.display = "none";
        text.innerHTML = "Show Background";
        createCookie( "background", "id", 365);
        }
} 
</script>    

</head>
<body onload ="readCookie('background'); eraseCookie('background'); createCookie('background','id', 365)">
<img id="back" src="http://miriadna.com/desctopwalls/images/max/Blue-sea-horizon.jpg" alt="" title="" /> 
<div style="border:0;position:fixed;top:300px;right:0;"> <a id="displayText" style="cursor: pointer;cursor: hand;" onClick="createCookie('background','id','365');toggle();">Hide Background</a></div>

</body>
</html>

I hope somebody can help me find a solution for my problem. (with javascript or php)
Thanks in advance.
Regards

Why don 't you use jquery it is easier and it has json that will talk to php easily

Hey dany12,

Thank you very much for your reply but I already solved my problem.

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.