Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
 
Or php (your tag), also, what do you mean 'load a specific page' –  Man of Snow 13 hours ago
3  
All of those you list (Javascript/Jquery/Ajax or HTML5) are clientside technologies, so they'd require a page to already be loaded; then you can replace it at midnight. With PHP (which you tagged with) you can do it serverside: simply pick if it's midnight or not, then readfile the appropriate file. You should decide on how long "midnight" is though; the traditional definition is that it's a point in time, closest JavaScript comes to it is milliseconds, and if you require users to hit the millisecond of midnight, not many will see your midnight page. –  Amadan 13 hours ago
1  
When you would tell us what your actual goal is, you might receive a much better answer. –  Philipp 13 hours ago
 
I know you say not cronjobs but can you say why not. If there is an issue with that we should give a better solution (cost, reliability etc) –  tim.baker 13 hours ago
1  
I just saw another interpretation of the question, where you just want to access a certain page, not show it to users, at midnight. In that case, curl in crontab is the right way to do it. –  Amadan 13 hours ago
show 1 more comment

2 Answers

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';
    }
}
share|improve this answer
 
So i take it, if i'd like to display a text then all i would need to do is implement your code, correct? –  Derek Harper 12 hours ago
 
Yes, at exactly 12:00 to 12:01 pm you would see 'yourText' displayed on the page, if you un-comment it out. To display text, just put document.onload = function() { document.body.innerHTML = 'yourText' } –  Man of Snow 11 hours ago
add comment

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
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.