Search code examples
javascriptphptiming

Time Script Within Header of Page


I need to time a script in my header to only display between 8:00 am and 5:30 PM Monday - Friday. It needs to remain in the header for templating purposes.

The script is the following:

<head><script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="http://www.example.com/chat/scripts/livechat.js"></script></head>

Now I would normally take the following approach, but it doesn't apply because this is not a div:

<script>// get current time
var d = new Date(), 
hours = d.getHours(),
mins = d.getMinutes();
day = d.getDay();   

// if day is mon-fri and time is between 8am and 5:30pm
if(0 < day < 6   
    && hours >= 8 
    && (hours < 17 || hours === 17 && mins <= 30)){

    $('.mydiv').show(); 
 };</script>

I'm confused as to how to make this work. Any help would be greatly appreciated.


Solution

  • I think you have small mistake in if. 0 < day < 6 is incorrect in JS.

    if((day > 0 && day < 6  ) 
        && hours >= 8 
        && (hours < 17 || hours === 17 && mins <= 30)){
    
        $('.mydiv').show(); 
     }