Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: load javascript depending time of day

  1. #1
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default load javascript depending time of day

    Hello
    I need a javascript what I can load different javascripts, depending on time of day. Is something feasible?


    Greetz

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Code:
    <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
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. The Following User Says Thank You to rangana For This Useful Post:

    Marquis (10-24-2008)

  4. #3
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    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

  5. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Code:
    <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
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  6. The Following 2 Users Say Thank You to rangana For This Useful Post:

    Marquis (10-26-2008),Maynard (10-28-2008)

  7. #5
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    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

  8. #6
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Yes, the script only alerts the created test file.

    If you want to create, change this part:
    Code:
    sScript='<script type="text/javascript" src="js'+js+'.js"><\/script>';
    alert(sScript); // Created script
    into:
    Code:
    var script=document.createElement('script');
    script.type='text/javascript';
    script.setAttribute('src','js'+js+'.js');
    document.getElementsByTagName('head')[0].appendChild(script);
    Hope that helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  9. The Following User Says Thank You to rangana For This Useful Post:

    Maynard (10-28-2008)

  10. #7
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    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

  11. #8
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    It's working fine at my end, ensure to refresh your page right after you change your time.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  12. #9
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    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

  13. #10
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Add highlighted on this part:
    Code:
    script.setAttribute('src','scripts/js'+js+'.js');
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •