Displaying multiple copies of the same Javascript

Status
Not open for further replies.

Hookster264

In Runtime
Messages
162
Location
Denver, CO
I am trying to build a webpage that includes the multiple copies of the same javascript. The script is for time zone clocks. I want to display a counting clock for multiple cities in a list. Some of the cities will be in the same time zone and I want a new clock with each city.

I resaved the script with varying Timezone offsets for each time zone and named them according to their time zone and daylight/standard.

When I recall each script with the <span> tag, I can only get one copy of each script to run.

Is there a way to run multiple copies of the same script on one page? Is there an easier way to call on the scripts than to use the span tag?

my script is below:
Code:
<script>
function mdt ()
{
var TimezoneOffset = -6
var localTime = new Date()
var ms = localTime.getTime() 
+ (localTime.getTimezoneOffset() * 60000)
+ TimezoneOffset * 3600000
var time = new Date(ms) 
var hour = time.getHours() 
var minute = time.getMinutes()
var second = time.getSeconds()
 
var temp = 
((hour < 10) ? "0" : "") + hour + ":"
if(temp.length==1) temp = " " + temp
temp += ((minute < 10) ? "0" : "") + minute + ":"
temp += ((second < 10) ? "0" : "") + second
document.getElementById("mdt").firstChild.nodeValue = temp;
setTimeout("mdt ()",1000)
}
</script>
 
<body onload="mdt ()">
And recall it in the body:
Current Time <br /><span id="mdt" class="time"> </span><br /> Mountain Daylight (MDT)
 
you would want to pass in a variable to the function. you would want the variable to set which elementid to update. the function would need to be modified to calculate each time zone. using a select case is prolly best for this. i think you can run multiple javascript functions in the onload attribute of the body tag. they must be separated by semicolons. example: onload="mdt('mdt');mdt('edt');mdt('cdt')"
 
I have modified the function var TimezoneOffset = -6 to -7 for Pacfic time zone and to -5 for Central. Each time I resaved it naming them pdt.js and cdt.js respectfully.

Then in the html I included <body onload="mdt (); cdt (); pdt ()">

In my <Head> I reference each script to the folder where it is saved. Then in the body of the code I can inlcude a <span> with id for each of the tree scripts and they all three will work.

What I am trying to do is to get multiple copies of each script to run throughout the page. Each unique ID can only be used once per page. Is there another way to call on the same script multiple times within the page without having to save multiple copies of the script and call on each one with its own uniqe ID tag?

Like resaving mdt.js to mdt2.js, mdt3.js, mdt4.js etc. This will work, but I was hoping to only refer to the one script each time.
 
starting from scratch, you'd want to create a function that allows one variable to be passed to it. This variable will set which elementid to work on.

Code:
function showTime(myID) {
//code
switch (myID) {
case "edt":
  //code for edt
  break;
case "mdt":
  //code for mdt
  break;
//etc, etc
}

}

then in the body tag you'll write onload="showTime('edt');showTime('mdt');"
This tells the function which elementid to work on.



edit - actually it'll prolly be easier for you to ignore the switch case above and just replace
document.getElementById("mdt").firstChild.nodeValue

with

document.getElementById(myID).firstChild.nodeValue


you'll need to add the variable to the recursive function call
 
Status
Not open for further replies.
Back
Top Bottom