New Posts  All Forums:Forum Nav:

Javascript help?

post #1 of 5
Thread Starter 
Code:
<html>
  <head>

    <script type="text/javascript" src="cookie.js"></script>

    <script type="text/javascript">
      var textColor;
          var backColor;

      function greetUser() {
        if (navigator.cookieEnabled)
          textColor = readCookie("your_textColor");
          backColor = readCookie("your_backColor");
        if (textColor)
                        alert("Hello the color " + textColor + " has been changed to the text.");   
                        document.getElementById('main').style.color= textColor;
                if(backColor)
                        alert("Hello the color " + backColor + " has been changed to the background color.");       
                        document.getElementById('main').style.backgroundColor= backColor;       
        else
          alert('Hello, I do not remember your information.');
      }

      function loadInfo() {
        if (textColor ) {
          alert("The text color " + textColor + " has been loaded. Thank you.");
                
            if (backColor) 
          alert("The background color " + backColor + " has been loaded. Thank you.");
            }
        else {
          textColor = prompt("What is your text color?", "Enter your color here.");
                  backColor = prompt("What is your background color?", "Enter your color here.");
                if (textColor) {
            alert("I see your text color is " + textColor + ". It has been changed.");
                                                document.getElementById('main').style.color= textColor; 
                        }
                if(backColor) {
                        alert("I see your background color is " + backColor + ". It has been changed.");
                                                document.getElementById('main').style.backgroundColor= backColor;
                        }
          if (navigator.cookieEnabled) {
              writeCookie("your_textColor", textColor, 24 * 7);
                          writeCookie("your_backColor", backColor, 24 * 7);
                        }
            else {
              alert("Cookies aren't supported/enabled in your browser, which means I won't remember you later. I'm sorry.");
                          }
                        }
                 }
        
                document.write("The current text color is " + textColor + ".");
                document.write("The current background color is " + backColor + ".");
                
    </script>
  </head>

  <body onload="greetUser(); loadInfo()" id="main">
  </body>
</html>


*******************************************************************

cookie.js
------------------

function writeCookie(name, value, days) {
  var expires = "";

  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
  }


  document.cookie = name + "=" + value + expires + "; path=/";
}u

function readCookie(name) {
  var searchName = name + "=";
  var cookies = document.cookie.split(';');
  for(var i=0; i < cookies.length; i++) {
    var c = cookies[i];
    while (c.charAt(0) == ' ')
      c = c.substring(1, c.length);
    if (c.indexOf(searchName) == 0)
      return c.substring(searchName.length, c.length);
  }
  return null;
}

function eraseCookie(name) {
  writeCookie(name, "", -1);
}



Basically I'm trying to make it so whatever the user puts in the alert box can be taken and used as a color towards text/background. I can't seem to get it though.. help? :x I'm trying to use "document.getElementById('main').style.color= textColor;" etc as you can see, but that doesn't work. I'm pretty bad at this so any help is appreciated.
150%
(17 items)
 
  
Reply
150%
(17 items)
 
  
Reply
post #2 of 5
Code:
<script language="JavaScript">
<!--
function changeBackgroundColor(color){
document.bgColor = color;
}
//-->
</script>


You can do this anywhere, I guess, so you won't have problems to call this from within your alertbox.

here is an example call to change it to blue:
Code:
javascript:changeBGC('#000099')

it should also work with:
Code:
javascript:changeBGC('blue')
    
CPUMotherboardGraphicsRAM
i5 750 @ 3.6 P7P55 EVO Gtx 670 DC2T 8gb @1600 cas 9 @1.5v 
Hard DriveOptical DriveCoolingOS
crucial m4 256gb + 3tb seagate none venomous-X + 2 P/P yate loons @5v 8 pro x64 + mint 14 cinnamon x32 
MonitorKeyboardPowerCase
PX2370 random Antec HPC-850 BX-500 aerocool 
MouseMouse PadAudio
CM Storm Spawn QcK+ Shure SRH 750DJ+FiiO E5 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i5 750 @ 3.6 P7P55 EVO Gtx 670 DC2T 8gb @1600 cas 9 @1.5v 
Hard DriveOptical DriveCoolingOS
crucial m4 256gb + 3tb seagate none venomous-X + 2 P/P yate loons @5v 8 pro x64 + mint 14 cinnamon x32 
MonitorKeyboardPowerCase
PX2370 random Antec HPC-850 BX-500 aerocool 
MouseMouse PadAudio
CM Storm Spawn QcK+ Shure SRH 750DJ+FiiO E5 
  hide details  
Reply
post #3 of 5
Thread Starter 
Quote:
Originally Posted by EduFurtado View Post

Code:
<script language="JavaScript">
<!--
function changeBackgroundColor(color){
document.bgColor = color;
}
//-->
</script>
You can do this anywhere, I guess, so you won't have problems to call this from within your alertbox.
here is an example call to change it to blue:
Code:
javascript:changeBGC('#000099')
it should also work with:
Code:
javascript:changeBGC('blue')

Thanks, works perfectly now ! thumb.gif +rep

One more question, what do I need to change above to make the cookies expire in one week?
150%
(17 items)
 
  
Reply
150%
(17 items)
 
  
Reply
post #4 of 5
Quote:
Originally Posted by iinversion View Post

Thanks, works perfectly now ! thumb.gif +rep
One more question, what do I need to change above to make the cookies expire in one week?

Where you have:
Code:
function writeCookie[B](name, value, days) [/B]{
  var expires = "";

  if (days) {
    var date = new Date(); //create a date object
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); //converts time to days
    expires = "; expires=" + date.toGMTString();
  }


  document.cookie = name + "=" + value + expires + "; path=/";
}u

This function receives three parameters: "(name, value, days)"
So the third is the number of days you want the cookie to last, in this case 7 days. All you have to do is pass a 7 in the third parameter when calling this function.

For example:
Code:
javascript:writeCookie("something","something","7")

By the way, are you sure that "u" at the end is supposed to be there?

Also, I'm not sure that function is mistake free. I would write it like this:
Code:
function writeCookie(name,value,days){
var exdate=new Date();
exdate.setDate(exdate.getDate() + days);
var c_value=escape(value) + ((days==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=name + "=" + c_value;
}
    
CPUMotherboardGraphicsRAM
i5 750 @ 3.6 P7P55 EVO Gtx 670 DC2T 8gb @1600 cas 9 @1.5v 
Hard DriveOptical DriveCoolingOS
crucial m4 256gb + 3tb seagate none venomous-X + 2 P/P yate loons @5v 8 pro x64 + mint 14 cinnamon x32 
MonitorKeyboardPowerCase
PX2370 random Antec HPC-850 BX-500 aerocool 
MouseMouse PadAudio
CM Storm Spawn QcK+ Shure SRH 750DJ+FiiO E5 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i5 750 @ 3.6 P7P55 EVO Gtx 670 DC2T 8gb @1600 cas 9 @1.5v 
Hard DriveOptical DriveCoolingOS
crucial m4 256gb + 3tb seagate none venomous-X + 2 P/P yate loons @5v 8 pro x64 + mint 14 cinnamon x32 
MonitorKeyboardPowerCase
PX2370 random Antec HPC-850 BX-500 aerocool 
MouseMouse PadAudio
CM Storm Spawn QcK+ Shure SRH 750DJ+FiiO E5 
  hide details  
Reply
post #5 of 5
Thread Starter 
No that "u" is definitely not supposed to be there. I probably accidentally hit a key when I was posting it in this thread. But wow, way easier than I thought ! Thanks a lot for the help, and I'll also use that writeCookie function that you suggested instead. biggrin.gifthumb.gif
150%
(17 items)
 
  
Reply
150%
(17 items)
 
  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding