Results 1 to 3 of 3

Thread: Need help with a very simple problem in Javascript Please.

  1. #1
    Join Date
    Feb 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with a very simple problem in Javascript Please.

    I am a beginner when it comes to Javascript so I was wondering if anyone could help me fuse my code. I have am using this code for cookies.

    Code:
    // Cookie Functions  ////////////////////  (:)
     
     
     
    // Set the cookie.
     
    // SetCookie('your_cookie_name', 'your_cookie_value', exp);
     
     
     
    // Get the cookie.
     
    // var someVariable = GetCookie('your_cookie_name');
     
     
     
    var expDays = 100;
     
    var exp = new Date(); 
     
    exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
     
     
     
    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" : "");
     
    }
     
     
     
    // cookieForms saves form content of a page.
     
    // use the following code to call it:
     
    //  <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">
     
     
     
    // It works on text fields and dropdowns in IE 5+
     
    // It only works on text fields in Netscape 4.5
     
     
     
     
     
    function cookieForms() {  
     
        var mode = cookieForms.arguments[0];
     
        
     
        for(f=1; f<cookieForms.arguments.length; f++) {
     
            formName = cookieForms.arguments[f];
     
            
     
            if(mode == 'open') {    
     
                cookieValue = GetCookie('saved_'+formName);
     
                if(cookieValue != null) {
     
                    var cookieArray = cookieValue.split('#cf#');
     
                    
     
                    if(cookieArray.length == document[formName].elements.length) {
     
                        for(i=0; i<document[formName].elements.length; i++) {
     
                        
     
                            if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
     
                            else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[formName].elements[i].checked = true; }
     
                            else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }
     
                            else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }
     
                        }
     
                    }
     
                }
     
            }
     
                    
     
            if(mode == 'save') {    
     
                cookieValue = '';
     
                for(i=0; i<document[formName].elements.length; i++) {
     
                    fieldType = document[formName].elements[i].type;
     
                    
     
                    if(fieldType == 'password') { passValue = ''; }
     
                    else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
     
                    else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }
     
                    else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }
     
                    else { passValue = document[formName].elements[i].value; }
     
                
     
                    cookieValue = cookieValue + passValue + '#cf#';
     
                }
     
                cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter
     
                
     
                SetCookie('saved_'+formName, cookieValue, exp);     
     
            }   
     
        }
     
    }
     
    //  End -->
    Ans I am using this code to hide/show a div element.

    Code:
    // Function to show div
     
    function showDiv(divid){
        if(document.getElementById(divid).style.display == 'none')
          document.getElementById(divid).style.display = 'block';
        }
     
    // Function to hide div
     
    function hideDiv(divid){
        if(document.getElementById(divid).style.display == 'block')
          document.getElementById(divid).style.display = 'none';
        }
    Can anyone help me correctly inject the code which hides shows a div element into this cookie script? I know this will be an easy fix for you Javascripting Pros

  2. #2
    Join Date
    Feb 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hello I have found out how to solve my problem I just need the correct syntax.
    I need to do the following:

    Inside the if() statement in your showdiv() function, call setcookie(), recording the fact that the div is shown.

    Inside the if() statment in your hidediv() function, call setcookie(),recording the fact that the div is hidden.

    When the page loads, read that cookie, and if it says the div is shown, then show the div. If it says the div is hidden, then hide the div.
    Can anyone help me write the correct code please?

  3. #3
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    easiest

    Code:
    <html>
    
    <head>
      <title></title>
    <style type="text/css">
    /*<![CDATA[*/
    #tst1 {
     position:relative;width:100px;height:100px;background-Color:red;
    }
    
    #tst2 {
     position:relative;display:none;width:100px;height:100px;background-Color:green;
    }
    
    /*]]>*/
    </style><script type="text/javascript">
    /*<![CDATA[*/
    var Days=1; // change 1 to the required days persistance
    
    function zxcCreateCookie(nme,v,days){
     document.cookie=nme+'='+v+';expires='+(new Date(new Date().getTime()+days*86400000).toGMTString())+';path=/';
    }
    
    function zxcReadCookie(nme){
     nme+='=';
     var split = document.cookie.split(';');
     for(var z0=0;z0<split.length;z0++){
      var s=split[z0];
      while (s.charAt(0)==' ') s=s.substring(1,s.length);
      if (s.indexOf(nme)==0) return s.substring(nme.length,s.length);
     }
     return null;
    }
    
    function Div(id){
     var div=document.getElementById(id);
     div.style.display=zxcSV(div,'display')!='none'?'none':'block';
     zxcCreateCookie(id,div.style.display,Days);
    }
    
    function zxcSV(obj,par){
     if (obj.currentStyle) return obj.currentStyle[par.replace(/-/g,'')];
     return document.defaultView.getComputedStyle(obj,null).getPropertyValue(par.toLowerCase());
    }
    
    
    function Restore(){
     var divs=document.getElementsByTagName('DIV');
     for (var z0=0;z0<divs.length;z0++){
      if (divs[z0].id&&zxcReadCookie(divs[z0].id)){
       divs[z0].style.display=zxcReadCookie(divs[z0].id);
      }
     }
    }
    
    /*]]>*/
    </script>
    </head>
    <body onload="Restore();">
    <input type="button" name="" value="Div tst1" onclick="Div('tst1');" />
    <input type="button" name="" value="Div tst2" onclick="Div('tst2');" />
    <div id="tst1" ></div>
    <div id="tst2" ></div>
    
    </body>
    
    </html>

  4. The Following User Says Thank You to vwphillips For This Useful Post:

    dissonantallure (05-10-2009)

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
  •