Log in

View Full Version : load javascript depending time of day



Marquis
10-23-2008, 10:58 PM
Hello
I need a javascript what I can load different javascripts, depending on time of day. Is something feasible?


Greetz

rangana
10-24-2008, 01:35 AM
<script type="text/javascript">
var date=new Date(),
sScript='<script type="text/javascript" src="js'+date.getHours()+'.js"><\/script>';
alert(sScript); // Created script
</script>


24 hours a day, 24 scripts naming js[1-24].js.

If it's 9am, then script will be js9.js

Marquis
10-24-2008, 07:36 PM
Hello
Thank you very much!

How can I do this with only 4 predefined times? 00.00 - 6.00, 6.00 - 12.00 , 12.00 - 18.00, 18.00 - 24. 00

thx

rangana
10-25-2008, 02:53 AM
<script type="text/javascript">
var date=new Date(),
hrs=date.getHours(),js='',sScript='';
// If 12am-6am - source equates to "jsfirst.js"
// If 7am-12pm - source equates to "jssecond.js"
// If 12pm-6pm - source equates to "jsthird.js"
// If it's 6pm and beyond, but don't reach 12am, then source equates to "jsfourth.js"
js=(hrs>=0&&hrs<=6)?'first':(hrs>6&&hrs<=12)?'second':(hrs>12&&hrs<=18)?'third':'fourth';
sScript='<script type="text/javascript" src="js'+js+'.js"><\/script>';
alert(sScript); // Created script
</script>


First shift (12am - 6am): jsfirst.js
Second Shift (7am - 12): jssecond.js
Third Shift (12-6pm): jsthird.js
Last Shift (6pm-11pm): jsfourth.js

Marquis
10-28-2008, 09:43 AM
Hi

Hmmm I can't get it to work. Maybe I didn't understand it right. I get only the alert, no javascript is loaded.

Can you help pls?

Greetz

rangana
10-28-2008, 10:17 AM
Yes, the script only alerts the created test file.

If you want to create, change this part:


sScript='<script type="text/javascript" src="js'+js+'.js"><\/script>';
alert(sScript); // Created script


into:


var script=document.createElement('script');
script.type='text/javascript';
script.setAttribute('src','js'+js+'.js');
document.getElementsByTagName('head')[0].appendChild(script);


Hope that helps.

Marquis
10-28-2008, 12:02 PM
Hi, thx for your reply

Now the script loads allways the jsfirst.js, no matter what time it is.

sry, i need more help.

Thx

rangana
10-28-2008, 12:16 PM
It's working fine at my end, ensure to refresh your page right after you change your time.

Marquis
10-28-2008, 02:18 PM
Hi

Yes, now it works! Just a last question: If i want to have the included scripts in sub directory, "scripts" where i must change the code?

Thx very much

rangana
10-29-2008, 12:14 AM
Add highlighted on this part:


script.setAttribute('src','scripts/js'+js+'.js');

Marquis
10-29-2008, 08:59 PM
Hmmm strange things happens

offline on local machine it works fine, running online it freezes the Browser. Testet in IE and Firefox., any idea why?

rangana
10-30-2008, 12:02 AM
Please provide a link to the page in question.

Nile
10-30-2008, 12:15 AM
What browser are you working? Also are you using the same browser that you are testing it local..?
And make sure that the script exists.

bokanegro
10-31-2008, 08:08 AM
Time of day with images TUTORIAL (http://javascript.about.com/library/bltod1.htm)