1

This question is more conceptual in nature then specific in terms of code.

I have a simple JavaScript that displays whether my company is open and our open hours on any given day of the week.

jQuery(document).ready(function($) {
    var d = new Date();

    var curDay = d.getDay();

    $('#curDay').html(curDay);

    if (curDay==1 || curDay==4) {
        $('#hours-operation').html("We are open today<br>from 11am - 6pm");
    } else if (curDay==3 || curDay==5) {
        $('#hours-operation').html("We are open today<br>from 11am - 9pm");
    } else if (curDay==0 || curDay==6) {
        $('#hours-operation').html("We are open today<br>from 10am - 6pm");
    } else {
        $('#hours-operation').html("We are closed today<br><br>");
    }

});

Lately we have had closings due to inclement weather on a semi regular basis. Is there a way to have an external entity change the script, for instance a form input? And have it remove itself when the clock moves to the next day, thereby resetting the script to it's original state? The best scenario would be not to edit the script itself at all.

2
  • save external entities in your database and when you are trying to display this list you can make an ajax call and get if any days are not open and set it. Commented Jan 9, 2014 at 16:17
  • You'll need server-side code to do what you're describing. Commented Jan 9, 2014 at 16:20

3 Answers 3

2

save external entities in your database and when you are trying to display this list you can make an ajax call and get if any days are not open and set it. I have described it as comment in my code. in your database you need to save this information which you will get by form:

 date, start time,endtime

Then in your javascript:

jQuery(document).ready(function($) {

   // make an ajax call and get data with current day.
   // check if it has any data for this day
  if (you have  data){
      set in your hours-operation
    }else
   {

      var d = new Date();     
      var curDay = d.getDay(); 
       $('#curDay').html(curDay);   
       if (curDay==1 || curDay==4) {
          $('#hours-operation').html("We are open today<br>from 11am - 6pm");
       } else if (curDay==3 || curDay==5) {
          $('#hours-operation').html("We are open today<br>from 11am - 9pm");
       } else if (curDay==0 || curDay==6) {
          $('#hours-operation').html("We are open today<br>from 10am - 6pm");
       } else {
          $('#hours-operation').html("We are closed today<br><br>");
        } 
    }

});

I think that makes sense :)

Sign up to request clarification or add additional context in comments.

1 Comment

So, I could set up a form, and have it input into the db. Then do as you mention and check for input based on the current day. If there is a field in the database from the current day, then display it via ajax, otherwise run the code from the script. I think that would work.
2

You'll need a bit more than just javascript/jquery to achieve that, that is a form, some kind of backend to store data and a web service which your javascript will query.

2 Comments

This is a comment, not an answer.
The site itself is in Wordpress so I have access to a mysql db if necessary.
0

I'm not sure to have understand all. however... Yes, you can override this code.

But

if you not want to change your script you need to add a script that override the behavior.

like:

 $(function({

      /*exec your code*/

      if (/* extraordinary condition true or false? */) {
            $('#hours-operation').html("We are closed today<br><br>");
      }
 });

but maybe is better to indent your code inside a function or inside a condition:

var dayOpen = function(){

     /*your code*/

}

$(function({
      if (/* extraordinary condition true or false? */) {
            dayOpen();

            /*or your code*/

      }else{
            $('#hours-operation').html("We are closed today<br><br>");
      }
 });

in any case I'm agree with other user. You need some dynamic information to build your /* extraordinary condition true or false? */.

For me there are two main ways:

  1. Use a Serve side API (PHP,JAVA,RUBY,ecc...) that you can call via ajax for know if today your business is open:

    $(function(){
        $.ajax({
            url : /* your API link*/,
            dataType : "json", /*[see example response at the and of the code] */
            success : function(res){
                if(res.problem) return $('#hours-operation').html(res.message+"<br><br>");
                /*your normal code here*/
            }
        })
    })
    

    response example:

    {
        problem : true, //or false if there are no problem with opening
        message : "Today we are closed for inclement weather"
    }
    
  2. Use a static file in your FTP. In this case you only need to call via ajax (HEAD) a file inside your webspace and if this file exist (response isn't 404) javascript knows that "today there is a extraordinary closing":

    $(function(){
        $.ajax({
            type : "head",
            url : /* link to your file*/,
            success : function(res){
                $('#hours-operation').html("Today we are extraordinarily closed<br><br>");
            },
            error : function(){
                /*no file NO PROBLEM*/
                /* your normal code here */
            }
        })
    })
    

    your file can be a JSON or a TXT for more information, my case is the simplest with the lowest work for server (HEAD CALL not GET)

I'm not english... so my English isn't good because right now i need to go and i can't read and correct my errors, i hope you can understand.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.