Writing dynamic content

Navigation

Skip navigation.

Site search

Site navigation

This next line varies with the time of day

Good .

The code is simple. Get the hour of the day. Depending on what that is, use document.write() to write a different word.

var theTime = new Date();
theTime = theTime.getHours();
if( theTime < 6 || theTime >= 21 ) {
    document.write( 'night' );
} else {
    if( theTime >= 6 && theTime < 12 ) {
        document.write( 'morning' );
    } else {
        if( theTime >= 12 && theTime < 18 ) {
            document.write( 'afternoon' );
        } else {
            document.write( 'evening' );
        }
    }
}

Use this form to rewrite the contents of this page.

Enter some new text to display:

This uses document.open() then document.write() to write the new content then uses document.close(). As soon as document.close() is written, the new content is displayed and the old page is completely overwritten. The old content is no longer accessible.

This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.