0

Someone here very kindly gave me this script to disable a 'send form' button at a certain time. It works great! Thanks again!

var expMsg = document.getElementById("expireMsg");
var subBtn = document.getElementById("subBtn");
var terminate = new Date("October 30, 2018 20:30:00");
//
function start() {
  checkForExpiration();
}
//
function checkForExpiration() {
  expMsg.innerHTML = "";
  subBtn.disabled = false;
  var currentDate = new Date();
  //
  if (currentDate > terminate) {
    subBtn.disabled = true;
    expMsg.innerHTML = "Sorry, but I said send the homework before 8:30pm today. You are too late.";
    return false;
  } else {
    return true;
  }
}
//
window.onload = start();

What I would like to do is use something similar to switch a link to a formpage on. This is just a homework page, no sensitive data or stuff.

To get to the formpage with the homework, you click a nice green rollover button on another page like this:

<li><a href="17week7.html">学期第七周<p>week7 </p></a></li>

I think the best way to achieve what I want is to make this button come live at a certain time. I think the above function could be modified to do this, but I don't have sufficient knowledge to do that.

Any tips, hints or links for this rank amateur please?

Martin
  • 22,212
  • 11
  • 70
  • 132
Pedroski
  • 433
  • 1
  • 7
  • 16

1 Answers1

1

Let's say I upload it today, but I want it to come live next Tuesday 26th February 2019

Source Data (the link you'd like to change):

<a href="17week7.html">学期第七周<p>week7 </p></a>

NOTE the <p> tags are not allowed inside <a> tags in HTML markup unless it is HTML5. It is however generally considered bad symantics, so I have removed your <p> tags for this example.

For ease I have also replaced your <li> with <div>.

So using your original code you can simply reframe it to Exceed the date set:

var linkMsg = document.getElementById("link");
    var kickoff = new Date("February 26, 2019 06:00:00");
    //

    function checkForValidation() {
        var currentDate = new Date();
        //
        if (currentDate > kickoff) {
           linkMsg.innerHTML = "<a href='17week7.html'>学期第七周 week7</a>";
        } 
    }
    //
    window.onload = checkForValidation();
   <div id='link'><a href='#'>This link is not yet valid</a></div>

Outputs:

When the page is loaded before the date specified:

  <div id='link'><a href='#'>This link is not yet valid</a></div>

When the page is loaded after the date specified:

  <div id='link'><a href='17week7.html'>学期第七周 week7</a></div>

What this does:

  • Add an id value to the container element, in this case <div> to indentify the link that will need updating when the date is correct.
  • When the window loads:
  • This gets the date of Now; and compares with the target date (hardcoded)
  • If the date of Now exceeds the target date...
  • ... Then the contents of the id are updated and the replacement contents shows the valid anchor link.
  • JQuery can do this in a neater way, but I have used your code given so you can more easily see what's going on and how it works.
Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • Thanks a lot! I can't actually try it now, gotta go, but I will try it later and let you know. Great! – Pedroski Feb 22 '19 at 22:36
  • 1
    All good! I set the date to yesterday, then uploaded the page to my host. Works great! Thanks very much! – Pedroski Feb 23 '19 at 04:46