Javascript
Cookie Switcher:
This script stores the value of the images to a cookie. Based on how many times the user visits the web page within a 30 day period will result in a new image displayed…
See in action (Refresh your browser or close and reopen to see the image change) Delete your cookies from internet options to start over!
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
var value = 0;
function getTheCookie(){
var x = document.cookie;
if (x == "")
{
document.getElementById("output").innerHTML = "You have not visited this page in the last thirty days!";
value = 1;
setTheCookie(value);
}
else
{
var cookie_pieces = x.split(';');
for(var i=0; i<cookie_pieces.length; i++){
var piece = trim(cookie_pieces[i]);
var a = piece.indexOf('=');
if (a == -1){
var key = piece;
var value = '';
}
else
{
var key = piece.substr(0,a);
value = piece.substr(a+1);
}
if (value == 1)
{
document.getElementById('image').src = "img/Ryanbw.jpg";
}
else if (value ==2)
{
document.getElementById('image').src = "img/chicago.jpg";
}
else if (value == 3)
{
document.getElementById('image').src = "img/Light.jpg";
}
else if (value == 4)
{
document.getElementById('image').src = "img/d4.jpg";
}
else if (value == 5)
{
document.getElementById('image').src = "img/AirBrush.jpg";
}
// This will Loop the pictures on every visit
//else
//{
// value = 0;
//}
//window.alert('Key: ' + key + " Value : "+ value);
document.getElementById("output").innerHTML = "You have been here " + value + " \"too many\" times.";
value++;
setTheCookie(value);
}
}
// end of if
} // end of the getTheCookie() function
function setTheCookie(value)
{
document.cookie = "NumberOfVisits=" + value + "; expires=" + exp + ";";
}
function trim(str){
// trim off leading spaces
while (str.charAt(0) == ' '){
str = str.substring(1);
}
//trim off trailing spaces
while (str.charAt(str.length-1) == ' '){
str = str.substring(0,str.length-1);
}
// return the trimmed value
return str;
}
Name Array():
This script prompts the user for a name entry. A dynamic array is created with as many inputs as necessary. (Pressing Enter while field is blank will dump the array).
See in action
function createSimpleLoopingArray()
{
// create a temporary array to hold values
var tempArray = new Array();
// create a variable to be used as a counter
var i = 0;
// create a variable to be used as a string
var tempString = "";
// get the initial input from the user
var userReply = window.prompt("What is your Name?","");
// check to see if the initial value is blank
if (userReply == "" || userReply == null)
{
// add to the temporary string
tempString = "Please enter a name. <a href='ArrayName.html'>Back</a>";
}
else
{
// while the user input is not empty, loop
// and get mor values
while (userReply != "")
{
// assign values to the temporary Array
tempArray[i] = userReply;
userReply = window.prompt("What is your Name?","");
i++;
}
tempString = "<strong>The Names Are:</strong> <br/><br/> "
if (tempArray.length == 1) {
tempString += tempArray[0];
tempString += "."
}
else
{
// iterate through the array to add values
// note: go through all items except the
// last item
for (i = 0; i < tempArray.length - 1; i++)
{
// add the values
tempString += tempArray[i];
tempString += "<br/> ";
} // end of for loop
// add the word 'and'
//tempString += " and "
// add the last item
tempString += tempArray[tempArray.length - 1];
// add a period to the end of the string
tempString += ". "
}
}
// send the string out to the user
var myNewWindow = window.open('','');
myNewWindow.document.write(tempString);
myNewWindow.document.close();
}
Comments