PDA

View Full Version : Need help with a script


Will
29.07.2001, 17:55
Okay, this is the fourth board this topic is on, so I'm beginning to think my inability to figure it out isn't because I'm stupid. I know it isn't dhtml, though I figured with all the js demi-gods in here, it couldn't hurt to try! Here goes:

I am using a simple array to automatically rotate items through the page (pretty much the same as "Tip Of the Day" scripts). The array inserts its content by calling from another script which formats the layout while containing easily configured vars. The two scripts look like this (this is the example for movies):

<script language="JavaScript"><!--
/*Format Movie Layout Script
credits and comments here*/

function writemovie()
{
var describe = "Place description here"
var dvd_asin = "0000000000"
var vhs_asin = "0000000000"
var image = "images/thismovie.jpg"
url_begin = "http://www.amazon.com/exec/obidos/ASIN/"
amazon_id = "/my-id"

code="<center>" + image + "</center>
";
code+="<font face='Verdana' size=-1>";
code+=describe
code+="

<center>Review And Buy
";
code+="" + "[ DVD ] " + " ('" + url_begin + dvd_asin + amazon_id + "')";
code+="" + "[ VHS ]" + " ('" + url_begin + vhs_asin + amazon_id + "')";
code+="</font></center></p>";
return code;
}
// -->
</script>

<script language="JavaScript"><!--
/*Item Of The Day Script
credits and comments here*/

//Make new array and get the current day
var item = new Array();
Stamp = new Date();
today = Stamp.getDate();
item[0]=writemovie()
item[1]=writemovie()
item[2]=writemovie()

document.write(item[today-1]);
// -->
</script>

This script runs flawlessly, though to make it run a different movie every day, I would need to add 300 lines of code by repeating the Format Movie script 31 times. This is obviously unacceptable, especially when I'll need to run three or four of these scripts on the same page (movies, books, music etc).

Someone gave me this array and for the life of me I seem unable to include it:

var days=new Array("Here You have to put whatever you want","calls to functions, variables, or simply put in all the code for the html");
function writemovie(day)
{
document.write(days[day-1]);
}
var day=new Date;
day=getDate();
writemovie(day);

If anyone has some marvelous ideas, I'm all ears!

Thanks a bunch-- Will

Dan
29.07.2001, 21:53
Well you do need to use arrays to store the movie info. Set up a 2D array like so:
<pre id=code><font face=courier size=2 id=code>
var movies = { {description, dvd_asin, vhs_asin, image},
{"Place description here","0000000000","0000000000","images/thismovie.jpg"}};</font id=code></pre id=code>Then if you want to show a certain movie for each day of the week use:
<pre id=code><font face=courier size=2 id=code>var day = new Date();
day = day.getDay(); //returns a number 0-6 (sunday=0)</font id=code></pre id=code>
Checkout this[/url:xbbp43jpjq] if you want to use day of the month or whatever.

Now your write_movie should look like this:
<pre id=code><font face=courier size=2 id=code>function writemovie()
{
var day = new Date();
day = day.getDay();
var describe = movies{day}{0}
var dvd_asin = movies{day}{1}
var vhs_asin = movies{day}{2}
var image = "" + movies{day}{3} + ""
url_begin = "http://www.amazon.com/exec/obidos/ASIN/"
amazon_id = "/my-id"
code="<center>" + image + "</center>
";
code+="<font face='Verdana' size=-1>";
code+=describe
code+="

<center>Review And Buy
";
code+="[url='" + url_begin + dvd_asin + amazon_id + "']" + "[ DVD ] " + " (http://www.devguru.com/Technologies/ecmascript/quickref/date.html)";
code+="" + "[ VHS ]" + " ('" + url_begin + vhs_asin + amazon_id + "')";
code+="</font></center></p>";
return code;
}</font id=code></pre id=code>

There ya go! Except change curly brackets for square brackets - UBB was messing up the code!

<font face='Courier New'><font color=maroon>»» Dan (dan@pupius.net)
»» www.pupius.net || [url="http://www.dhtmlcentral.com/resources/redirect.asp?linkid=23"]www.endoflow.com[/url:xbbp43jpjq]</font id=maroon></font id='Courier New'>

Edited by - Dan on 29 July 2001 7:55:35

Will
30.07.2001, 00:15
Hi Dan--

Thank you very much for the quick and enlightened response. Interestingly, someone on another board yielded nearly the identical solution, which looks like:

function Movie(des,dvd,vhs,img,url,amz){
this.des=des; this.dvd=dvd; this.vhs=vhs
this.img=img; this.url=url; this.amz=amz
}
movies=new Array("Day 0",
new Movie("Description Here","0100000000","0000000000","images/thismovie.jpg","http://www.amazon.com/exec/obidos/ASIN/","/my-id"),
)
d= new Date()
d=d.getDate()
code="<center>" + movies[d].img + "</center>
";
code+="<font face='Verdana' size=-1>";
code+=movies[d].desc
code+="

<center>Review And Buy
";
code+=".url + movies[d].dvd + movies[d].amz + "']" + "[ DVD ] " + " ('" + movies[d)";
code+=".url + movies[d].vhs + movies[d].amz + "']" + "[ VHS ]" + " ('" + movies[d)";
code+="</font></center></p>";
document.write(code)

This method creates a pretty big flatfile (around 6k), though I'm thinking I can incorporate your suggestion of using getDay() with four scripts. This breaks everything up a bit and saves some bandwidth. Since I'm already running 50k of external scripting my page (a coolmenu plus other various stuff), I need to be a bit sparing.

Since I have plenty of server space while pressing my loading limit on the page. at one point I began thinking, "Why can't I just call separate scripts named as functions with something like:

function writemovie1();
{
code="<script src='scripts/movie1.js'>";
return code
}

and reference it the normal way with item[0]=(writemovie1());

And it failed. It would, of course, mean 30 external scripts, but they could have been easily managed. Any idea why this flopped?

Again, thanks an awful lot for your help!

--Will

Will
30.07.2001, 03:28
For those interested, just thought I'd take a minute and throw in what I have presently settled upon for the format of this script:

<script language="JavaScript">
<!--//
function Movie(des,dvd,vhs,img,url,amz){
this.des=des; this.dvd=dvd; this.vhs=vhs
this.img=img; this.url=url; this.amz=amz
}
desc01="Description Here"
desc02="Description Here"
desc03="Description Here"

var url = "http://www.amazon.com/exec/obidos/ASIN/"
var id = "your-id"

movies=new Array("Day 0",
new Movie(desc01,"0100000000","0010000000","images/thismovie.jpg",url,id),
new Movie(desc02,"0200000000","0020000000","images/thismovie.jpg",url,id),
new Movie(desc03,"0300000000","0030000000","images/thismovie.jpg",url,id),
)
d= new Date()
d=d.getDate()
code="<center>" + movies[d].img + "</center>
";
code+="<font face='Verdana' size=-1>";
code+=movies[d].des
code+="

<center>Review And Buy
";
code+=".url + movies[d].dvd + movies[d].amz + "']" + "[ DVD ] " + " ('" + movies[d)";
code+=".url + movies[d].vhs + movies[d].amz + "']" + "[ VHS ]" + " ('" + movies[d)";
code+="</font></center></p>";
document.write(code)
// -->
</script>

I apologize Dan, yet I seem presently unable to work out a practical way to include the getDay argument. I've been kicking around the notion of making this thing random by day (which would obviously require a cookie), and your solution might transpond nicely to a situation like that. Should I use it, I'll be sure to credit you.

Thanks again-- Will