Reputation: 31
Ok, this is the script I'm using right now:
<script>
u = new Date(document.lastModified);
m = u.getMonth() + 1;
d = u.getDate();
y = u.getFullYear();
t = "Last update";
document.writeln(t + ": " + d + "/" + m + "/" + y);
</script>
The script is working but the problem is that this script is on my index.html file. In stead of the script i would like to have a DIV:
<DIV id="LastMod"></DIV>
And the javascript in a seperate .js file. Any help would be appreciated, Thanks!
Upvotes: 1
Views: 106
Reputation: 37796
With the help of Intl.DateTimeFormat you can format a date object anyway you like
also don't forget to declare your variables (and don't use short names)
// setting i18n to undefined will make the formater use the os preferred setting
var i18n = 'en-GB'
var date = new Date(document.lastModified)
var formated = new Intl.DateTimeFormat(i18n).format(date)
document.getElementById('lastMod').innerText = "Last update: " + formated
<div id="lastMod"></div>
Upvotes: 0
Reputation: 475
You can put the js in a separate js file, then use the call to an external Javascript file:
<div id="LastMod"></div><script type="text/javascript" src="js/myscript.js"></script>
You can place the link to your js file in the head of your html document (best practice) or in your body as per code example above(deprecated but works fine).
Upvotes: 0
Reputation: 330
save this with name myscripts.js
----------------------
u = new Date(document.lastModified);
m = u.getMonth() + 1;
d = u.getDate();
y = u.getFullYear();
t = "Last update";
var total =(t + ": " + d + "/" + m + "/" + y);
document.getElementById("LastMod").innerHTML = total;
----------------------
and your index.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>test</title>
<script src="myscripts.js"></script>
</head>
<body>
<DIV id="LastMod"></DIV>
</body>
</html>
Upvotes: 0
Reputation: 2106
u = new Date(document.lastModified);
m = u.getMonth() + 1;
d = u.getDate();
y = u.getFullYear();
t = "Last update";
document.getElementById('LastMod').innerHTML =t + ": " + d + "/" + m + "/" + y;
<div id="LastMod"></div>
Try It Once
Upvotes: 3
Reputation: 74738
Instead of document.write
use this:
u = new Date(document.lastModified);
m = u.getMonth() + 1;
d = u.getDate();
y = u.getFullYear();
t = "Last update";
document.querySelector("#LastMod").textContent = t + ": " + d + "/" + m + "/" + y;
Now you can put your script in a external file and make a reference to it in the bottom of the page because page is not been in the DOMContentLoaded
block.
If you put it in the block then you can put your script at the top in the head of the page.
Upvotes: 3
Reputation: 518
<script>
u = new Date(document.lastModified);
m = u.getMonth() + 1;
d = u.getDate();
y = u.getFullYear();
t = "Last update";
document.getElementById("LastMod").innerHTML = t + ": " + d + "/" + m + "/" + y;
</script>
add this script AFTER the element in the dom, or you need to wait for document ready (when script is executed the element isn't loaded yet)
Upvotes: 1