Reputation: 827
Simple question.
I'm newbie on javascript and html.
I have a file index.html like:
<html>
<head>
<link rel=StyleSheet href="beauty.css" type="text/css">
<script src="myjs.js"></script>
</head>
<body>
<p id="foo"></p>
</body>
</html>
and this is my js file:
var d=new Date();
var month = d.getMonth()+1;
var data = d.getDate() + "/" + month + "/" + d.getFullYear();
function showdata(){
return data;
}
document.getElementById("foo").innerHTML=showdata();
I understand that I need to reference the html, but I do not have idea how to do that.
Upvotes: 0
Views: 84
Reputation: 31040
You might want
window.onload = function() {
var d=new Date();
var month = d.getMonth()+1;
var data = d.getDate() + "/" + month + "/" + d.getFullYear();
function showdata(){
return data;
}
document.getElementById("foo").innerHTML=showdata();
}
You also might want to specify type="text/javascript"
in your <script>
tag.
Upvotes: 1
Reputation: 20439
Try moving the script tag to the bottom of the page. Right before closing </body>
tag. Something like this:
<html>
<head>
<link rel=StyleSheet href="beauty.css" type="text/css">
</head>
<body>
<p id="foo"></p>
<script src="myjs.js"></script>
</body>
</html>
The problem is that your javascript code was executed before the paragraph element was part of the page.
Upvotes: 0