Click to See Complete Forum and Search --> : date formula
continuo
09-27-2005, 01:04 PM
Hello,
I am new here, and I probably wouldn't be here if I didn't have a problem.
I have an html file with Javascript ... but it's not doing what it's suppose to be doing. I have to start from scratch.
The idea of the script is to display an image based on the current date. Here is the formula in words:
month + day + year = abcd (a 4 digit number)
then a + b + c + d = xy (a 1 or 2 digit number)
If xy > 21, then x + y = z (a 1 or 2 digit number)
If xy ? 21, then xy = z (a 1 or 2 digit number)
If z = 1, then show image 1
If z = 2, then show image 2
If z = 3, then show image 3
and so forth until 21
Example 1: September 17, 2005
9 + 17 + 2005 = 2031
2 + 0 + 3 + 1 = 6
Since 6 < 21, then 6 = 6
If 6, then show image 6
Example 2: December 25, 1935
12 + 25 + 1939 = 1976
1 + 9 + 7 + 6 = 23
Since 23 > 21, then 2 + 3 = 5
Since 5 < 21, then 5 = 5
If 5, then show image 5
Any suggestions would be greatly appreciated! Thank you.
muneepenee
09-27-2005, 02:55 PM
wi dont yu yuze dae-av-month tu deside wich pik tu sho?
continuo
09-27-2005, 04:59 PM
wi dont yu yuze dae-av-month tu deside wich pik tu sho?
I'm actually not perfectly sure what you said ... but the images I want to display are judged from the date formula I previously decribed.
I have a script with a formula, but there's something wrong and I'd rather start over then go over it with a fine tooth comb.
:(
Ultimater
09-27-2005, 05:22 PM
The end result is an image e.g. 1.jpg or 2.jpg (today it should be spitting out 7.jpg)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 STRICT//EN" "http://www.w3.org/TR/HTML401/strict.dtd">
<html dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title></title>
<style type="text/css"><!--
body{
background-color:rgb(255,255,200);
color:rgb(50,50,255);
font: 10pt arial, helvetica, sans-serif;
margin: 15px;
padding: 0;
}
img{
background-color: white;
color: red;
}
--></style>
<script type="text/javascript">
Array.prototype.addAndJoin=function(){
var A=this;
var r=0;
for(var i=0;i<A.length;i++){
r+=(new Number(A[i]));
}
return String(r);
}
var D=new Date()
var m=D.getMonth()+1;
var d=D.getDate();
var y=D.getFullYear();
var abcd=new String(m+d+y);
var xy=abcd.split("").addAndJoin();
var z;
if(xy>21)z=xy.split("").addAndJoin();
else z=xy;
</script>
</head>
<body>
<p>
<script type="text/javascript">
document.write("The number picked was:"+z+"\n<br><br>\n");
document.write("<img src=\""+z+".jpg\" alt=\"The image "+z+".jpg didn\'t load\"");
</script>
</body>
</html>
Ultimater
09-27-2005, 05:32 PM
If xy > 21, then x + y = z (a 1 or 2 digit number)
If xy ? 21, then xy = z (a 1 or 2 digit number)
I'm assumming that the ? means <= correct?
continuo
09-27-2005, 05:38 PM
I'm assumming that the ? means <= correct?
It should be the "equal-or-less-than" sign ... I guess certain characters on Macs can't be viewed on PCs.
The code you had looks a lot more simple than the one I tried to use, but it has some customization (with the background color and such).
This is the code I had, which included the location of the image files (which are in a folder titled "images" which is in the same folder as the html file):
<html>
<head>
<title>Image Viewer</title>
<script type="text/javascript">
function imageshow()
{
var userdate = new Date();
var result = userdate.getDate() + (userdate.getMonth() + 1) + userdate.getFullYear();
var numtext = "'" + result + "'";
var dig1 = parseInt(numtext.substr(1,1));
var dig2 = parseInt(numtext.substr(2,1));
var dig3 = parseInt(numtext.substr(3,1));
var dig4 = parseInt(numtext.substr(4,1));
var result = dig1 + dig2 + dig3 + dig4;
if (result > 21)
{
* *numtext = "'" + result + "'";
* *dig1 = parseInt(numtext.substr(1,1));
* *dig2 = parseInt(numtext.substr(2,1));
* *result = dig1 + dig2;
}
var imgarray = Array(21);
imgarray[0] = "/images/1.png";
imgarray[1] = "/images/2.png";
imgarray[2] = "/images/3.png";
imgarray[3] = "/images/4.png";
imgarray[4] = "/images/5.png";
imgarray[5] = "/images/6.png";
imgarray[6] = "/images/7.png";
imgarray[7] = "/images/8.png";
imgarray[8] = "/images/9.png";
imgarray[9] = "/images/10.png";
imgarray[10] = "/images/11.png";
imgarray[11] = "/images/12.png";
imgarray[12] = "/images/13.png";
imgarray[13] = "/images/14.png";
imgarray[14] = "/images/15.png";
imgarray[15] = "/images/16.png";
imgarray[16] = "/images/17.png";
imgarray[17] = "/images/18.png";
imgarray[18] = "/images/19.png";
imgarray[19] = "/images/20.png";
imgarray[20] = "/images/21.png";
var im = document.getElementById("userimage");
im.src = imgarray[result];
}
</script>
</head>
<body onload="imageshow()">
<img id="userimage" src="">
</body>
</html>
Does anything look wrong?
Ultimater
09-27-2005, 05:49 PM
var dig1 = parseInt(numtext.substr(1,1));
var dig2 = parseInt(numtext.substr(2,1));
var dig3 = parseInt(numtext.substr(3,1));
var dig4 = parseInt(numtext.substr(4,1));
Should be
var dig1 = parseInt(numtext.substr(0,1));
var dig2 = parseInt(numtext.substr(1,1));
var dig3 = parseInt(numtext.substr(2,1));
var dig4 = parseInt(numtext.substr(3,1));
Change:
var numtext = "'" + result + "'";
into
var numtext = ""+ result;
and change
if (result > 21)
{
* *numtext = "'" + result + "'";
* *dig1 = parseInt(numtext.substr(1,1));
* *dig2 = parseInt(numtext.substr(2,1));
* *result = dig1 + dig2;
}
into:
if (result > 21)
{
numtext = "" + result;
dig1 = parseInt(numtext.substr(0,1));
dig2 = parseInt(numtext.substr(1,1));
result = dig1 + dig2;
}
give me a minute to make the hi-lights
Ultimater
09-27-2005, 05:54 PM
K, finished with the hi-lights -- you can read it now.
continuo
09-27-2005, 06:04 PM
Okay, I made the changes (the astericks shouldn't have been there, there was a copy-paste issue)
=)
So, it's not working ... but maybe it's because of the tags after the script. Should there be something between the "" in
<img id="userimage" src="">
?
Or maybe there's just something else wrong?
Maybe I should remove the forward slashes where it tells the images location in the script?
Ultimater
09-27-2005, 06:09 PM
The script is indeed spitting out the numbers though, see for your self by temporarily changing:
var im = document.getElementById("userimage");
im.src = imgarray[result];
into:
alert(result);
alert(imgarray[result]);
The rest depends on the location of your images.
You should use absolute urls anyways:
im.src ="http://www.mywebsite.com"+ imgarray[result];
continuo
09-27-2005, 06:13 PM
So, for image 7, I would have
im.src ="/images/7.png"+ imgarray[result];
because the images will be located on the user's computer in a folder which is in the same folder as the html file.
Ultimater
09-27-2005, 06:17 PM
You would replace "http://www.mywebsite.com" with the location to the folder e.g. "file://c:/mystuff"
Or you could do as you asked and remove the preceeding slash of all your array elements -- but if you take that route and in the future if you ever need to move your HTML file then you would be forced to also move the images.
continuo
09-27-2005, 06:21 PM
since this is will be Mac program, is there a generic location I can use, since I don't think Macs use "c:"?
It just needs to know that the images will always be in the "images" folder in the application folder regardless of where it is.
Ultimater
09-27-2005, 06:24 PM
For a mac? hmmm... not sure.... try replacing the forward slash with a period.
The situation is as simple as getting the following to display an image:
<img src="/images/7.png" alt="7.png">
or
<img src=".images/7.png" alt="7.png">
Whichever or whatever works and is a correct URL reference to the image. Try viewing the properties of the image on your computer to obtain the absolute URL or the image on your mac.
Ultimater
09-27-2005, 06:32 PM
Did you give this a try yet?
<img src="images/7.png" alt="7.png">
If none of the above three HTML tags work, post your question in the HTML forum.
continuo
09-27-2005, 06:42 PM
Did you give this a try yet?
<img src="images/7.png" alt="7.png">
If none of the above three HTML tags work, post your question in the HTML forum.
It ocurred to me that that tag has to be universal - it depends on the result.
So, at the end of the script, it was
im.src = imgarray[result];
and after the script, at the end of the html, it was
<img id="userimage" src="">
and I wasn't sure if there should be something inbetween the "".
So, when I open the html file, it displays a "?" icon, which means the browser isn't finding the image file. I think there's a problem with the image location.
Here is the current code:
<html>
<head>
<title>Image Viewer</title>
<script type="text/javascript">
function imageshow()
{
var userdate = new Date();
var result = userdate.getDate() + (userdate.getMonth() + 1) + userdate.getFullYear();
var numtext = "'" + result;
var dig1 = parseInt(numtext.substr(0,1));
var dig2 = parseInt(numtext.substr(1,1));
var dig3 = parseInt(numtext.substr(2,1));
var dig4 = parseInt(numtext.substr(3,1));
var result = dig1 + dig2 + dig3 + dig4;
if (result > 21)
{
numtext = "'" + result;
dig1 = parseInt(numtext.substr(0,1));
dig2 = parseInt(numtext.substr(1,1));
result = dig1 + dig2;
}
var imgarray = Array(21);
imgarray[0] = "/images/1.png";
imgarray[1] = "/images/2.png";
imgarray[2] = "/images/3.png";
imgarray[3] = "/images/4.png";
imgarray[4] = "/images/5.png";
imgarray[5] = "/images/6.png";
imgarray[6] = "/images/7.png";
imgarray[7] = "/images/8.png";
imgarray[8] = "/images/9.png";
imgarray[9] = "/images/10.png";
imgarray[10] = "/images/11.png";
imgarray[11] = "/images/12.png";
imgarray[12] = "/images/13.png";
imgarray[13] = "/images/14.png";
imgarray[14] = "/images/15.png";
imgarray[15] = "/images/16.png";
imgarray[16] = "/images/17.png";
imgarray[17] = "/images/18.png";
imgarray[18] = "/images/19.png";
imgarray[19] = "/images/20.png";
imgarray[20] = "/images/21.png";
var im = document.getElementById("userimage");
im.src = imgarray[result];
}
</script>
</head>
<body onload="imageshow()">
<img id="userimage" src="">
</body>
</html>
Ultimater
09-27-2005, 06:55 PM
Do me a favor and post the following HTML exactly how it reads in the HTML (http://webdeveloper.com/forum/forumdisplay.php?s=&daysprune=-1&f=2) forum:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 STRICT//EN" "http://www.w3.org/TR/HTML401/strict.dtd">
<html dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Image Viewer</title>
</head>
<body>
<p>
<img src="images/7.png" alt="7.png">
</body>
</html>
Ask 'em why it is not displaying your image on a macintosh. There are no futher JavaScript issues here, just a matter of know how to server a Mac with HTML.
continuo
09-27-2005, 07:05 PM
posted
:o
So, by looking at the script in my previous code post, is the script fine? Even this:
var im = document.getElementById("userimage");
im.src = imgarray[result];
and also the location of the images in the script?
Thanks!
Ultimater
09-27-2005, 07:19 PM
Actually, no it isn't.
var numtext = "'" + result;
numtext = "'" + result;
Take out the reded-in area.
continuo
09-27-2005, 07:31 PM
Actually, no it isn't.
var numtext = "'" + result;
numtext = "'" + result;
Take out the reded-in area.
Okay, just did that, but still getting the "?" when it loads.
I suspect the problem has something to do with the tags after the script, but we'll see.
Thanks for all of the tweaking and fixing! I'm hoping to get better at this.
:rolleyes:
continuo
09-27-2005, 08:09 PM
posted
:o
So, by looking at the script in my previous code post, is the script fine? Even this:
var im = document.getElementById("userimage");
im.src = imgarray[result];
and also the location of the images in the script?
Thanks!
in addition to the above, someone replied about the code I posted in the html forum:
try these 2 things:
i) end your </p> tag after the image. if that works....no need to carry on.
ii) change <!DOCTYPE> to transistional.
but I am uncertain about how it relates to my code.
Ultimater
09-27-2005, 08:20 PM
His suggestions don't relate to the problem in any way and his "corrections" would actually make the code worse. The code I have given you to post into the HTML forum used perfect HTML and is in no way flawed except for the path to the Image -- which is why I asked you to post it there in the first place. I even ran it through the HTML validator, it's perfectly legal HTML.
continuo
09-27-2005, 08:26 PM
His suggestions don't relate to the problem in any way and his "corrections" would actually make the code worse. The code I have given you to post into the HTML forum used perfect HTML and is in no way flawed except for the path to the Image -- which is why in the firstplace I had to post it there.
So, is it the locations which are in the script (telling the result to display which iimage), or the location which is after the script?
Ultimater
09-27-2005, 08:29 PM
If you solve one, you would have solved the other. If you get the Image to display properly using simple HTML, then I would know the URL of the images to code-in for you in the more complicated JavaScript.