Guest ShadowIce Posted June 7, 2009 Share Posted June 7, 2009 Hello all Hi all, I figured out a way to have different cookie names for different parts of a website. Now I have but 1 small problem. the name of the cookie that my code should be returning is index[0], index[1], etc... PROBLEM is that index[0] SHOULD = whatever number the selected index is, and should load it when u refresh the page, thus displaying the value of that index that was selected the 1st time. You will need an image called "myimage.jpg".here's the code: <html><head><title>bgchanger</title></head><body onload="loadall();"><script type="text/javascript">showfile("?");function showfile() {var pathname = location.href;var filename = pathname.substr(pathname.lastIndexOf("/")+1,pathname.length);var filename = filename.substr(filename,filename.lastIndexOf(".") );if(filename == ""){var filename = pathname.substr(pathname.lastIndexOf("/")+1,pathname.length);var filename = filename.substr(filename,filename.lastIndexOf(".") )+"?";}return filename;}function showpath() {var pathname = location.pathname;pathname = pathname.substr(1,pathname.length);}var backgroundImages =[ "","myimage.jpg" ];var backgroundNames =[ "Default","My image" ];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"+"_"+showfile());loadOptions(document.TheForm.favSelect);loadpositionopt(document.TheForm.favSelect2);setDiv("position"+"_"+showfile(),"repeat"+"_"+showfile());setIndex();alert(document.cookie);}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 saveindex(){ var favIndex = document.TheForm.favSelect.selectedIndex; var favIndex2 = document.TheForm.favSelect2.selectedIndex; if(favIndex != "" || favIndex != ""){ if(favIndex){ DeleteCookie("index"+"["+favIndex+"]"+"_"+showfile()+"["+favIndex+"]"); }else if(favIndex2){ DeleteCookie("index"+"["+favIndex2+"]"+"_"+showfile()+"["+favIndex2+"]"); } } SetCookie("index"+"["+favIndex+"]"+"_"+showfile()+"["+favIndex+"]", favIndex, exp); SetCookie("index"+"["+favIndex+"]"+"_"+showfile()+"["+favIndex2+"]", favIndex2, exp);}function savepos(){ var favPos = document.forms.TheForm.favSelect2.value; if ( favPos == "" ) return; SetCookie("position"+"_"+showfile(), favPos, exp);location.href = location.href;}function savecolor(){ var favColor = document.forms.TheForm.favSelect3.value; if ( favColor == "" ) return; SetCookie("color"+"_"+showfile(), favColor, exp);}function saverep(rep){ var favrep = rep; if ( favrep == "" ) return; SetCookie("repeat"+"_"+showfile(), favrep, exp);}function setColor(cookie){document.getElementById("about").style.backgroundColor=GetCookie(cookie);}function setIndex(){for (var x = 0; x < document.TheForm.favSelect.length; x++) {document.TheForm.favSelect.selectedIndex = GetCookie("index"+"_"+showfile()+"["+x+"]");}for (var x = 0; x < document.TheForm.favSelect2.length; x++) {document.TheForm.favSelect2.selectedIndex = GetCookie("index"+"["+x+"]"+"_"+showfile()+"["+x+"]");} alert("document.TheForm.favSelect.selectedIndex="+document.TheForm.favSelect.selectedIndex);alert("document.TheForm.favSelect2.selectedIndex="+document.TheForm.favSelect2.selectedIndex);}function setDiv(cookie,cookie2){document.getElementById("about").style.backgroundPosition=GetCookie(cookie);document.getElementById("about").style.backgroundRepeat=GetCookie(cookie2);}function saveImage(){ var favImage = document.forms.TheForm.favSelect.value; if (favImage == "") DeleteCookie("position"+"_"+showfile()); SetCookie("image"+"_"+showfile(), favImage, exp); getDivBG("image"+"_"+showfile());location.href = location.href;}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(); saveindex();">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(); saveindex();">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); }}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 type="text/javascript">menu();</script></table></td></div></body></html> ANY help is GREATLY appreciated! any criticism is DEFINITELY ignored! ~SI~ Link to comment Share on other sites More sharing options...
justsomeguy Posted June 8, 2009 Share Posted June 8, 2009 These loops are not checking for the correct cookie names: for (var x = 0; x < document.TheForm.favSelect.length; x++) {document.TheForm.favSelect.selectedIndex = GetCookie("index"+"_"+showfile()+"["+x+"]");}for (var x = 0; x < document.TheForm.favSelect2.length; x++) {document.TheForm.favSelect2.selectedIndex = GetCookie("index"+"["+x+"]"+"_"+showfile()+"["+x+"]");} Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.