We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,695 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Set Cookies after toggle from background image to background color and back

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

2
Contributors
2
Replies
1 Week
Discussion Span
10 Months Ago
Last Updated
3
Views
Question
Answered
Coderunner
Newbie Poster
12 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

dany12
Posting Whiz in Training
223 posts since Aug 2010
Reputation Points: 21
Solved Threads: 15
Skill Endorsements: 1

Hey dany12,

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

Regards

Coderunner
Newbie Poster
12 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 10 Months Ago by dany12

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3092 seconds using 2.7MB