前のページメニュー次のページ

ファイルの更新年月日を表示

よく「Last Updated on XX/XX/XX」とか書いてあるやつです。CGIが使えない人は手書きでやるしかなかったですが、JavaScriptを使うと手でいちいち書かなくても、ファイルが更新されると自動的に更新年月日も変わるようになります。

ブラウザーの種類によってはスクリプトのソースが表示できない場合があるようなので、ここだけソースも同時に見られるようにしました。

(使用例1)

スクリプトは以下の通りです。

<script type="text/javascript">
<!-- JavaScript小技集
document.write("Last updated on " + document.lastModified +".");
// end of JavaScript -->
</script>

(使用例2)

スクリプトは以下の通りです。

<script type="text/javascript">
<!-- JavaScript小技集
(function() {
var update = new Date(document.lastModified);
var theMonth = update.getMonth() + 1;
var theDate = update.getDate();
var theYear = update.getFullYear();
document.writeln("<I>Last updated: " + themonth + "/" + thedate + "/" + theyear + "</I>");
})();
// end of javascript -->
</script>

(使用例3)

スクリプトは以下の通りです。

<script type="text/javascript">
<!-- JavaScript小技集
(function() {
var update = new Date(document.lastModified);
var theMonth = update.getMonth() + 1;
var theDate = update.getDate();
var theYear = update.getFullYear();
theYear = theYear - 1988;
document.writeln("最終更新日:平成" + theYear + "年" + theMonth + "月" + theDate + "日");
})();
// end of JavaScript -->
</script>

(使用例4)

スクリプトは以下の通りです。

<script type="text/javascript">
<!-- JavaScript小技集
(function() {
var update = new Date(document.lastModified);
var theMonth = update.getMonth() + 1;
var theDate = update.getDate();
var theYear = update.getFullYear();
document.writeln("最終更新日:" + theYear + "年" + theMonth + "月" + theDate + "日");
})();
// end of JavaScript -->
</script>