PDA

View Full Version : Need help creating different names for different cookies..



ShadowIce
06-02-2009, 11:58 PM
Hi all! :) I've been doing yet more research on cookies, and I have discovered 1 small flaw in my code..

What the problem would be is that whenever i have the same code for 2 different pages, i need to be able to have different cookie names. For example, if im on index.html , and i want to save that background to 1 cookie, named say for example "cookieimage1", then it will save the cookie. BUT if im on say another page like aboutus.html, and i want to save to a TOTALLY different cookie with the same name, like say "cookieimage2", then it will save the cookie TO ANOTHER NAME.

How would i create a function to return cookienamehere+x, where x = a number from 1 to infinite, or cookienamechangehere, where its a different name for each page entirely?

please note, u will need to create a folder named "images", and change the link(s) and the name(s) of the image(s) in the script whose variables are: backgroundImages, & backgroundNames... You will need an image called "MyImage.jpg", or whatever you changed it to.

here's the code:


<html>

<head><title>bgchanger</title></head>

<body onload="loadall();">

<script>
var thecookie;
var cookiename;
var backgroundImages =
[ "",
"images/MyImage.jpg" ];
var backgroundNames =
[ "MyDefault",
"MyImage" ];
var backgroundColors =
[ "red",
"green",
"blue" ];
var backgroundColornames =
[ "red",
"green",
"blue" ];
var backgroundPositions =
[ "top Left",
"top Center",
"top Right",
"Left",
"Center",
"Right",
"bottom Left",
"bottom Center",
"bottom Right" ];
var backgroundPositionnames =
[ "top Left",
"top Center",
"top Right",
"Left",
"Center",
"Right",
"bottom Left",
"bottom Center",
"bottom Right" ];

function loadall(){
getDivBG("image");
loadOptions(document.TheForm.favSelect);
loadpositionopt(document.TheForm.favSelect2);
if(thecookie="repeat"){setDiv(get_name("repeat"));}
if(thecookie="position"){setDiv(get_name("position"));}
//loadcoloropt(document.TheForm.favSelect3);
//setColor(get_name());
}

function getname(){
var cookiename;
cookiename = [ "image",
"position",
"repeat",
"color" ];
for(var x=0;x<=cookiename.length-1;x++){
thecookie = cookiename[x];
}
return thecookie;
}

function get_name(cookie){
thecookie = cookie;
return thecookie;
}

function loadOptions( into )
{
for ( var b = 0; b < backgroundImages.length; ++b )
{
into.options[into.options.length] =
new Option( backgroundNames[b], backgroundImages[b] );
}
}

function loadpositionopt( into1 )
{
for ( var c1 = 0; c1 < backgroundPositions.length; ++c1 )
{
into1.options[into1.options.length] =
new Option( backgroundPositionnames[c1], backgroundPositions[c1] );
}
}

function loadcoloropt( into2 )
{
for ( var b1 = 0; b1 < backgroundColors.length; ++b1 )
{
into2.options[into2.options.length] =
new Option( backgroundColornames[b1], backgroundColors[b1] );
}
}

function savepos(){
var favPos = document.forms.TheForm.favSelect2.value;
if ( favPos == "" ) return;
SetCookie(get_name(), favPos, exp);
}

function savecolor(){
var favColor = document.forms.TheForm.favSelect3.value;
if ( favColor == "" ) return;
SetCookie(get_name(), favColor, exp);
}

function saverep(rep){
var favrep = rep;
if ( favrep == "" ) return;
SetCookie(get_name(), favrep, exp);
}

var expDays = 365;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function setColor(cookie){
document.getElementById("about").style.backgroundColor=GetCookie(cookie);
}

function setDiv(cookie){
if(cookie == "position"){
document.getElementById("about").style.backgroundPosition=GetCookie(cookie);
}else if(cookie == "repeat"){
document.getElementById("about").style.backgroundRepeat=GetCookie(cookie);
}
}

function saveImage()
{
var favImage = document.forms.TheForm.favSelect.value;

SetCookie(get_name(), favImage, exp);
getDivBG(get_name());
}


function menu(){
document.write('<form name="TheForm">\n'
+'<td valign="top" style="width: 25%; height: 100%;"><br><hr><Center><span id="menutitle">News Menu</span></Center><hr><br>\n'
+'<span id="opt1">Background Image: </span><select style="width: 100%; height: 10;" name="favSelect" onchange="getDivsBG(this); saveImage();">\n'
+' <option value="">--choose--</option>\n'
+' </select></font>\n'
+'<span id="opt2">Background Position: </span><select style="width: 100%; height: 10;" name="favSelect2" onchange="getPosBG(this); savepos();">\n'
+' <option value="">--choose--</option>\n'
+' </select>\n'
+'<!--<span id="opt3">Background Color: </span><select style="width: 100%; height: 10;" name="favSelect3" onchange="check(); getColorBG(this); savecolor();">\n'
+' <option value="">--choose--</option>\n'
+' </select>--></td>\n'
+'</form>\n');
}

var expDays = 365;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function GetCookie(name)
{
var cookies = document.cookie.split(/; /g);
var arg = name + "=";
for ( var c = 0; c < cookies.length; ++c )
{
var ck = cookies[c];
if ( ck.indexOf(arg) == 0 )
{
var temp = ck.split(/=/);
return unescape(temp[1]);
}
}
return "";
}

function SetCookie (name, value)
{
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;

document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

function DeleteCookie (name)
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function getBodyBG(cookie,cookie2,cookie3)
{
document.body.style.backgroundImage="url('" + GetCookie(cookie) + "')";
document.body.style.backgroundPosition=GetCookie(cookie2);
document.body.style.backgroundRepeat=GetCookie(cookie3);
}

function getColorBG(bgcolor){
if(bgcolor != null){
document.getElementById("about").style.backgroundColor=GetCookie(bgcolor);
}
}

function getPosBG(bg){
if(bg != null){
document.getElementById("about").style.backgroundPosition=GetCookie(bg);
location.href = location.href;
}
}

function getDivsBG(pic)
{
if ( pic != ""){
document.getElementById("about").style.backgroundImage="url('" + pic + "')";
//document.getElementById("blahnews").style.backgroundImage="url('" + pic + "')";
//document.getElementById("blahnews").style.backgroundImage="url('" + pic + "')";
}
}

function getDivBG(cookie)
{
document.getElementById("about").style.backgroundImage="url('" + GetCookie(cookie) + "')";
//document.getElementById("blahnews").style.backgroundImage="url('" + GetCookie(cookie) + "')";
}

function getnews(newsbody,newsdata){

checkxmlhttprequest();

var txtFile = new XMLHttpRequest();
txtFile.open("GET", newsdata, true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) { // Makes sure the document is ready to parse.
if (txtFile.status === 200) { // Makes sure it's found the file.
allText = txtFile.responseText;
//lines = txtFile.responseText.split(/\r?\n/g); // Will separate each line into an array
document.getElementById(newsbody).innerHTML = allText;
} else {
document.getElementById(newsbody).innerHTML = 'News file not found';
}
}
}
txtFile.send();

}

function gettitle(newstitle,title_data){

var txtFile = new XMLHttpRequest();
txtFile.open("GET", title_data, true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) { // Makes sure the document is ready to parse.
if (txtFile.status === 200) { // Makes sure it's found the file.
allText = txtFile.responseText;
lines = txtFile.responseText.split("\n"); // Will separate each line into an array
document.getElementById(newstitle).innerHTML = lines;
} else {
document.getElementById(newstitle).innerHTML = '<span id=\"newsnotfound\">News file not found</span>';
}
}
}
txtFile.send();
}

function checkxmlhttprequest(){
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
} else {
alert('Sorry, your browser doesn\'t support XMLHTTPRequests');
return null;
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert('Sorry, your browser doesn\'t support XMLHTTPRequests');
return null;
}
}
</script>

<div id="about" style="width: 400px; height: 300px; background-Color: #44CFFC;">
</div>
<script>menu();</script>
</table></td>
</div>
</body>
</html>

ANY help is GREATLY appreciated! :)

Thanks! :)

~SI~

Torin Mai
06-03-2009, 06:03 AM
I would use the document.url property myself, that way the name would stay the same everytime you go to the page. Just come up with some sort of function to cut off the last bit of the url, ditch the file type and the cookie would be named cookiename_index for index.php. or something along those lines.

As for images and such, I'm not sure what you're asking but it sounds like what you're looking for is PHP or some other server side script, not javascript. Javascript can't modify files by itself.