PDA

View Full Version : More HTML stuff - javascript problem


Corona688
02-13-2005, 04:39 AM
I'm trying to add a 'freshness indicator' to my page; if it's less than a week old it's green, older than a week, darker green; older than a month, yellow; older than a year, red. I've got those colors in my style sheet, and have been trying to make a small javascript that fulfills this function:
<script language='Javascript'>
<!--
var curdate = new Date;
var yearold = curdate;
var monthold = curdate;
var weekold = curdate;
var update = new Date(document.lastModified);
var updstr = update.getDate() + "/" + update.getMonth() + "/" +
(update.getYear()+1900);

yearold.setYear(curdate.getYear()-1);
monthold.setMonth(curdate.getMonth()-1);
weekold.setDate(curdate.getDate()-7);

if(update <= yearold)
document.write("<span class='off'>"+updstr+"</span>");
else if(update <= monthold)
document.write("<span class='off'>"+updstr+"</span>");
else if(update <= monthold)
document.write("<span class='stale'>"+updstr+"</span>");
else if(update <= weekold)
document.write("<span class='okay'>"+updstr+"</span>");
else
document.write("<span class='fresh'>"+updstr+"</span>");
//-->
</script> But no matter the age of the page itself, the date and freshness is always brand new! What's going on here? Would use of server-side-includes screw this up?

xouper
02-13-2005, 07:34 AM
Corona688: ... But no matter the age of the page itself, the date and freshness is always brand new!
If your server is not supplying the Last-Modifed header info to the client browser, then using document.lastmodified won't work. If this happens, then MSIE defaults to "today" and netscape defaults to 1970.

You can use the following link to check if your server is supplying the appropriate header info:

http://www.delorie.com/web/headers.html

For example, the server hosting http://randi.org/ does not send the Last-Modified header. Neither does the server for http://www.freethought-forum.com/forum/index.php?

The server hosting my website http://xoup.net does send it.

If your server is supplying the correct Last-Modified header info and you are still having problems, then we can look at other things.

ceptimus
02-13-2005, 11:47 AM
Can your server run server-side scripts? Not everyone has javascript enabled. I'm thinking that PHP would be a better option, if it's available to you.

xouper
02-13-2005, 11:56 AM
ceptimus: Can your server run server-side scripts? Not everyone has javascript enabled. I'm thinking that PHP would be a better option, if it's available to you.
Good point. I too would recommend php over javascript for this particular task, if that's an option. Then it doesn't matter if the server sends the Last-Modified header or not, you just use the file date on the server.

Corona688
02-13-2005, 07:21 PM
Apparently I can't get last-modified headers from apache when I'm using server-side includes, so it always just gives me today. Server-side scripts are definitely enabled, as well as server-side includes, but I simply do not know PHP.

I'm also guessing I can't embed PHP in my included navigation header the same way I can embed javascript, just like I can't embed server-side includes inside my included navigation header, ergo adding PHP would mean modifying every last page on my website instead of one single navigation bar. That is the main reason I wanted the evil javascript :(

Oh well. Thanks anyways guys! :)

Corona688
02-13-2005, 07:51 PM
Hey! I'm getting closer! Apparently it's still possible to get last-modified info using server-side includes but I have to tell apache WHICH file it should come from. Something like <!--#flastmod file="freshness.html"--> will print the last-modified date, even if inside a server-included file... now to figure out how to get it into the headers

[edit] Got it! Apparently that's what the xbithack bit is for, it needs the group-executable bit set to tell which files it should sent last-modified headers for, because it might say a page has not been modified even when files it includes have been. Thanks everyone!

...And now I am back to my original problem. The javascript is now getting the correct last-modified dates, yet it does not work. The date comparisons must be messing up somewhere, but I have no idea how...

Corona688
02-13-2005, 10:18 PM
I figured it out! For those with javascript enabled, here's a small blurb (http://burningsmell.org/freshness.html). For those without javascript, the lack of the feature will not deface the page in any way, I only use javascript for optional frills.

xouper
02-14-2005, 01:13 AM
Corona688: ... I simply do not know PHP.
PHP as a language is not much different than javascript. You can put php code in your html file same as you do javascript. It just runs on the server side instead of the client side.


I'm also guessing I can't embed PHP in my included navigation header the same way I can embed javascript, just like I can't embed server-side includes inside my included navigation header, ergo adding PHP would mean modifying every last page on my website instead of one single navigation bar. That is the main reason I wanted the evil javascript :(
I use php on my website instead of SSI because php gives me much more flexibility. The way I use php for all the common elements of my pages means I do not have to modify each and every one of my pages when I want to change any of those common elements. In case you were wondering. :)

Anyway, glad to hear you solved your problem.