Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Need help with cookie background function..

  1. #1
    Join Date
    Mar 2009
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with cookie background function..

    Hi all I've been researching cookies a bit, and I attempted to create a function that if you select a background name from a drop down list or anything for that matter, it will save the background in the cookie, and display it in a div background for later use. Now I can't quite figure out why there is a problem with this script, but the error checker tells me there is.

    Heres the code:

    Code:
    <script type="text/javascript">
    
    //Get Cookie
    
    function get_cookie(Name) {
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
    offset += search.length
    // set index of beginning of value
    end = document.cookie.indexOf(";", offset);
    // set index of end of cookie value
    if (end == -1) end = document.cookie.length;
    returnvalue=unescape(document.cookie.substring(offset, end))
    }
    }
    return returnvalue;
    }
    
    function setbg(what){
    document.getElementById("mydiv").style.backgroundimage=url(what); 
    document.cookie="backgroundimage="+what
    }
    
    if (get_cookie("bg")!="")
    document.getElementById("mydiv").style.backgroundimage=get_cookie("bg")
    
    </script>
    
    <form name="a1">
    <p><select name="a2" size="1">
    <option selected value="NA">Select background</option>
    <option value="myimage.gif">My Image</option>
    </select>
    <div id="mydiv"></div><a href="javascript:setbg(document.a1.a2.options[document.a1.a2.selectedIndex].value)">Set Bg</a></p>
    </form>
    ANY help is GREATLY appreciated!

    Thanks!

    ~SI~

  2. #2
    Join Date
    May 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Is there a website that this code is currently up on?
    I may be able to find the "problem" with the Debugger in IE

  3. #3
    Join Date
    Mar 2009
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    no, but if u save it as a .html file using notepad, and opening it up, it should activate the code

  4. #4
    Join Date
    May 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I got this to work, so if you can manipulate it how you want, then I hope it works for you.

    PHP Code:
    <script type="text/javascript">

    //Get Cookie

    function get_cookie(Name) {
    var 
    search Name "="
    var returnvalue "";
    if (
    document.cookie.length 0) {
    offset document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
    offset += search.length
    // set index of beginning of value
    end document.cookie.indexOf(";"offset);
    // set index of end of cookie value
    if (end == -1end document.cookie.length;
    returnvalue=unescape(document.cookie.substring(offsetend))
    }
    }
    return 
    returnvalue;
    }

    function 
    setbg(what){
        var 
    nBG document.getElementById(what).value;
        
    document.getElementById("myStyles").innerHTML="&nbsp;<style>.mydivBG{ background-image: url("+nBG+")}</style>";
        
    document.getElementById("mydiv").className="mydivBG";
        return;
    }

    if (
    get_cookie("bg")!="")
    document.getElementById("mydiv").style.backgroundimage=get_cookie("bg")

    </script>

    <select name="a2" size="1" id="bgs">
    <option selected value="NA">Select background</option>
    <option value="myimage.gif">My Image</option>
    </select>
    <a href="javascript:void(0);" onclick="setbg('bgs');">Set Bg</a>

    <br /><Br /><br /><br />
    <div id="myStyles"></div>
    <div id="mydiv">There needs to be a big gap in here to view the background!</div> 

  5. #5
    Join Date
    Mar 2009
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks. but theres 1 problem. When I close my browser and come back to the site, it doesnt show the background..

  6. #6
    Join Date
    May 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Does your browser delete the cookies once closed?
    Or does it happen when your are on the same site but move to another page?

  7. #7
    Join Date
    Mar 2009
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    It does it when i move to another page.

  8. #8
    Join Date
    May 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    K, its either the Cookies not being set/pulled properly

    OR

    The JavaScript is messing it up, so lets assume its this first because its a easier fix.

    Replace the
    PHP Code:
    if (get_cookie("bg")!="")
    document.getElementById("mydiv").style.backgroundimage=get_cookie("bg"

    With
    PHP Code:
    if (get_cookie("bg")!="")
    var 
    nBG=get_cookie("bg")
    document.getElementById("myStyles").innerHTML="&nbsp;<style>.mydivBG{ background-image: url("+nBG+")}</style>";
    document.getElementById("mydiv").className="mydivBG"

  9. #9
    Join Date
    Mar 2009
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Still didnt work..

  10. #10
    Join Date
    Mar 2009
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    can u modify this to work with background images? it seems to save it when i go to another page, then i go back to the page

    Code:
    <script>var expDays = 30;
    var exp = new Date(); 
    exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
    function color(){
    var favColor = GetCookie('color');
    if (favColor == null) {
    favColor = prompt("What is your favorite background color?");
    SetCookie('color', favColor, exp);
    }
    document.bgColor=favColor;
    return favColor;
    }
    function set(){
    favColor = prompt("What is your favorite background color?");
    SetCookie ('color', favColor, exp);
    }
    function getCookieVal (offset) {  
    var endstr = document.cookie.indexOf (";", offset);  
    if (endstr == -1)    
    endstr = document.cookie.length;  
    return unescape(document.cookie.substring(offset, endstr));
    }
    function GetCookie (name) {  
    var arg = name + "=";  
    var alen = arg.length;  
    var clen = document.cookie.length;  
    var i = 0;  
    while (i < clen) {    
    var j = i + alen;    
    if (document.cookie.substring(i, j) == arg)      
    return getCookieVal (j);    
    i = document.cookie.indexOf(" ", i) + 1;    
    if (i == 0) break;   
    }  
    return null;
    }
    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();
    }</script>
    Thanks!

    ~SI~

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •