Dumb javascript question ... call function from a seperate file?

LordSnailz

Diamond Member
Nov 2, 1999
4,821
0
0
Okay, so I dl'd this javascript that I want to use. I stucked it to my index.html file and it works but is there a way to put the script in another file and have my index.html call the function from the file?

If so, how would I do it?
Here's the function call ... dunno how to tell it that it's in a file?

function setcountdown(theyear,themonth,theday){yr=theyear;mo=themonth;da=theday}
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
put your function inside a file, for example: myscript.js

on top of the page, replace the javascript block that defines the function (i assume it's inside your html's <head>) with this one:
<script type="text/javascript" src="myscript.js"></script>

and when you want to use the function, just call it up in your html (after you included it)
<script type="text/javascript">setcountdown();</script>
 

LordSnailz

Diamond Member
Nov 2, 1999
4,821
0
0
Not sure if I'm calling it correctly, here's what I put in my myscript.js file --
<script language="JavaScript1.2">

/*
Dynamic countdown Script- © Dynamic Drive (www.dynamicdrive.com)
For full source code, 100's more DHTML scripts, and TOS,
visit http://www.dynamicdrive.com
*/


function setcountdown(theyear,themonth,theday){
yr=theyear;mo=themonth;da=theday
}
//////////CONFIGURE THE COUNTDOWN SCRIPT HERE//////////////////

//STEP 1: Configure the countdown-to date, in the format year, month, day:
setcountdown(2005,06,25)

//STEP 2: Change the two text below to reflect the occasion, and message to display on that occasion, respectively
var occasion="Our Wedding!"
var message_on_occasion="The BIG Day!"

//STEP 3: Configure the below 5 variables to set the width, height, background color, and text style of the countdown area
var countdownwidth='780px'
var countdownheight='100px'
var countdownbgcolor='#DC745C'
var opentags='<font face="Georgia" fontSize="100" color="white">'
var closetags='</font>'

//////////DO NOT EDIT PASS THIS LINE//////////////////

var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
var crosscount=''

function start_countdown(){
if (document.layers)
document.countdownnsmain.visibility="show"
else if (document.all||document.getElementById)
crosscount=document.getElementById&amp;&amp;!document.all?document.getElementById("countdownie") : countdownie
countdown()
}

if (document.all||document.getElementById)
document.write('<span id="countdownie" style="width:'+countdownwidth+'; background-color:'+countdownbgcolor+'"></span>')

window.onload=start_countdown


function countdown(){
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
futurestring=montharray[mo-1]+" "+da+", "+yr
dd=Date.parse(futurestring)-Date.parse(todaystring)
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
//if on day of occasion
if(dday<=0&amp;&amp;dhour<=0&amp;&amp;dmin<=0&amp;&amp;dsec<=1&amp;&amp;todayd==da){
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write(opentags+message_on_occasion+closetags)
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML=opentags+message_on_occasion+closetags
return
}
//if passed day of occasion
else if (dday<=-1){
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write(opentags+"Occasion already passed! "+closetags)
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML=opentags+"Occasion already passed! "+closetags
return
}
//else, if not yet
else{
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write(opentags+dday+ " days, "+dhour+" hrs, "+dmin+" mins, and "+dsec+" secs left!"+

closetags)
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML=opentags+dday+ " days, "+dhour+" hrs, "+dmin+" mins, and "+dsec+" secs left!"+closetags
}
setTimeout("countdown()",1000)
}
</script>

<ilayer id="countdownnsmain" width=&amp;{countdownwidth}; height=&amp;{countdownheight}; bgColor=&amp;{countdownbgcolor}; visibility=hide><layer id="

countdownnssub" width=&amp;{countdownwidth}; height=&amp;{countdownheight}; left=0 top=0></layer></ilayer>



here is how I'm calling it
<script type="text/javascript">setcountdown(2005,06,25);</script>


What am I doing wrong?
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Ah, i didn't get the chance to look at it until just now.
new year and all, you know... ,)

Anyways, i took a look in your script, and here's how it should be done.

1. The parts from <script language="JavaScript1.2"> to </script> should be put inside your external file (test.js). Do not include the opening script and closing script tags, though, since you'd do that by specifying this in the header of your html file:

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

2. Following the line above, initialize the HTML page to specify your date you want to count to:
<script type="text/javascript">setcountdown(2005,01,06);</script>

It doesn't matter whether you put it inside the <head> or <body>, but personally i prefer the <head>, alongside the call in point number 1.

3. Inside the <body> of your script, include the layer things to print the result:

<ilayer id="countdownnsmain" width=&amp;{countdownwidth}; height=&amp;{countdownheight}; bgColor=&amp;{countdownbgcolor}; visibility=hide><layer id="countdownnssub" width=&amp;{countdownwidth}; height=&amp;{countdownheight}; left=0 top=0></layer></ilayer>

That should do it. I tested it just now ,)
let us know if you're still confused.
have fun -)