/    Sign up×
Community /Pin to ProfileBookmark

Selecet bg image -> write cookie -> read cookie

Hi there,

I use a popup window to select a background image for the main window.

[B]I want to set a cookie[/B]
a) to store the value of the choosen image, and
b) to read the cookie onload and set the stored image

Any ideas?
Thank you!

P.S.:
I write below:
a) the popup window code (that chooses the background image), and
b) the cookie code i have found

———————– START – popup window code ———————
<html>
<head>
<script language=”javascript” type=”text/javascript”>
function changebg(my)
{
opener.document.getElementById(“testdiv”).src = my;
}
</script>
</head>
<body>
<img src=”images1.jpg” width=”100″ height=”100″ onClick=”changebg(this.src)”/>
<img src=”images2.jpg” width=”100″ height=”100″ onClick=”changebg(this.src)” />
</body>
</html>
———————– END – popup window code ———————

———————– START – cookie code ———————

<script language=”javascript” type=”text/javascript”>

function getCookie(NameOfCookie)
{ if (document.cookie.length > 0)
{ begin = document.cookie.indexOf(NameOfCookie+”=”);
if (begin != -1)
{ begin += NameOfCookie.length+1;
end = document.cookie.indexOf(“;”, begin);
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 ();
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=Thu, 01-Jan-70 00:00:01 GMT”;
}

}

</script>

———————– END – cookie code ———————

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@UI-ZEIKVKNov 23.2007 — Check this

test.html
[code=html]
<html>
<head>

<script type="text/JavaScript" src="test.js" language="JavaScript"></script>
<script type="text/JavaScript" language="JavaScript">
var imgElm;
function checkCookie(val)
{
imgElm = document.getElementById('testdiv');
cookieVal = readCookie(val);
if(cookieVal != '')
{
imgElm.src = cookieVal;
}
}
</script>
</head>
<body onload="checkCookie('img')">
<a href="#" onclick="window.open('test1.html')">aa</a>
<div>
<img id="testdiv">
</div>
</body>
</html>

[/code]



test1.html
[code=html]
<html>
<head>
<script type="text/JavaScript" src="test.js" language="JavaScript"></script>
<script language="javascript" type="text/javascript">
function changebg(my)
{
createCookie( 'img', my, 1);
opener.document.getElementById("testdiv").src = my;
}
</script>
</head>
<body>
<img src="image1.gif" width="100" height="100" onClick="changebg(this.src)"/>
<img src="image2.gif" width="100" height="100" onClick="changebg(this.src)" />
</body>
</html>
[/code]


test.js
[code=html]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);
}[/code]


Cheers!
Copy linkTweet thisAlerts:
@DimisDauthorNov 23.2007 — Hi UI-ZEIKVK

Thank you very much for the answer, it works excellent !!!!!!? ?

You are great!!!

cheers to u tooo!!!!
Copy linkTweet thisAlerts:
@UI-ZEIKVKNov 26.2007 — You are welcome!
×

Success!

Help @DimisD spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...