简体   繁体   中英

Load a specific page at midnight using Javascript/Jquery/Ajax or HTML5?

This sounds a bit orthodox, i'll admit.

However i was wondering if there was a way to load a specific website using Javascript/Jquery/Ajax or HTML5?

Reason i ask is because i'm aware there's a method for using cronjobs, but i'm curious to know if there's another method that does not require cron jobs in order for it to function.

By load a specific page i'm referring to anything really, perhaps a set of words or an iframe.

  • Derek

You would have to have a page already loaded, so let's say this is the javascript in index.html :

var now = new Date(),
    then = new Date(
        now.getFullYear(),
        now.getMonth(),
        now.getDate(),
        0,0,0),
    diff = now.getTime() - then.getTime();

that will see the milliseconds since midnight. Now the code below will redirect to a page (or text) if it's within 1 minute of midnight:

document.onload = function()
{
    var now = new Date(),
        then = new Date(
            now.getFullYear(),
            now.getMonth(),
            now.getDate(),
            0,0,0),
        diff = now.getTime() - then.getTime();
    if(diff <= 60000)
    {
        //Un-comment first line to go to page, second to change text of page
        //window.location.href = 'pageToLoad.html';
        //document.body.innerHTML = 'yourText';
    }
}

Like you already know there are cron jobs (server side) wich allow you to execute some php scripts at a precise time on your server/host.

If you want that your users see different pages at a certain hour of the day(or a specific time) by loading different content with ajax . Here is a very short example.

1.ajax function for modern browsers(chrome,safari,ie10,ios,android..)

2.time check

3.get night or day content.

function ajax(a,b,c){//url,function,just a placeholder
 c=new XMLHttpRequest;
 c.open('GET',a);
 c.onload=b;
 c.send()
}

var h=new Date().getHours();
// if it's after 12pm & before 6am it returns night else day.
nightday=(h>0&&h<6?'night':'day')+'.php';

//get the data from file 'day.php' or 'night.php'
ajax(nightday,function(){
 //this.response is the content
 console.log(this.response);
});

if you want to execute this just once:

 window.onload=function(){
  var c=new XMLHttpRequest,h=new Date().getHours();
  c.open('GET',(h>0&&h<6?'night':'day')+'.php');
  c.onload=function(){console.log(this.response)};
  c.send()
 }

And from here there are now various ways to check what time it is.

on every click,on some specific clicks,setTimeout(bad),setIntervall(bad)..and much more.

night.php

<?php
//load the content for night
?>

day.php

<?php
//load the content for day
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM