0

I'm trying to make reminder. In HTML i'd have 2 text boxes (one for date and other for something like "Today is your grandmothers birthday!") and one button that executes the function. So when user enters date and text, and clicks button, in JavaScript function is created that will display "Today is your grandmothers birthday!" in some paragraph, when that day comes.
I know how to make function reminder ()that will display "Today is your grandmothers birthday!" on given date, but i don't know how to make function that will add my reminder() function to my JavaScript document.
Is that even possible ?

8
  • 2
    It doesn't make much sense. Just make the code always check to see which message(s) to show. Commented Apr 5, 2015 at 16:13
  • 1
    I don't think 'changing Javascript content' is an accurate qualification for this functionality. Commented Apr 5, 2015 at 16:17
  • I think this is what you are looking for stackoverflow.com/questions/12964262/… Commented Apr 5, 2015 at 16:19
  • 1
    If you're trying to store information on a server, you need server side code (like PHP). If you want to store the information locally, you can use localstorage. Anyway, I would't suggest to make dynamic functions, just store key:value pairs with a date and a message. Commented Apr 5, 2015 at 16:34
  • Thanks Anshuman , I think that will do the trick. [link](stackoverflow.com/questions/12964262/… ) I like second answer better, but i don't understand what is "use strict" for. Commented Apr 5, 2015 at 16:40

1 Answer 1

0

This is a bit silly and probably is not what you want, but you can definitely rewrite a function in JavaScript to be anything you want. Example below: first time you run the code, it will prompt you and change the function itself to do something totally different and using closure, it will capture the initial date and message to be used on the next call of itself.

(function() {
  'use strict';
  
  function Reminder() {
 
    var aDate  = prompt('Enter date (dd/mm/yyyy): ');
    var msg = prompt('Enter message to aassociate with the date: ');
  
    var inputDate = new Date(aDate);
  
    Reminder = function()  {
        var todaysDate = new Date();
        if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
          alert(msg);
        }        
    };
 
    return Reminder;
  }
  
  //Firs time around ask for the data and dynamically change the function...
  Reminder();
  
  //call it again... this time around it should remind you and no longer pompt...
  Reminder();
  
}());

As the comments said, what you really want is probably to store the inputs in some sort of storage and when the page loaded, it will retrieve the data from the storage and do the calculation to whether remind the user of things that was previously saved.

This is definitely not it, but since you asked if you can rewrite javascript from javascript, yes you can.

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

1 Comment

I've seen this kind of pattern being used as a caching mechanism where on first call it will go back to server and load the data and then using the rewrite, captured whatever value retrieved and just serve that value the next time around it is requested and saving the page from going to go to server every single time it is needd.

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.