I'm new to HTML and JS and I'm trying to make an alert when a button is pressed on the webpage that says "Good day" or "Good evening" The problem is that I need to do it on an external JS and I'm having trouble doing it. Here is the code I have so far:
var hour = new Date().getHours();
var greeting;
if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
alert(greeting)
}
Thanks in advance!
Edit: I'm doing this for a project, and it says that no script tags should be contained in index.html Here is my current index.html:
<html>
<head>
<title>page</title>
<link rel="stylesheet" href="style.css"
</head>
<body onload="loading()">
<div id="musicartists"></div>
<script src="script.js"></script>
<h1 onclick="greeting()">Greetings!</h1>
<button onclick="birthday()">My birthday</button>
<button onclick="myfunction()">Time based greeting</button>
<h3 id="musicartists">My favorite sports:</h3>
<p class="sports">Skiing</p>
<p class="sports">Swimming</p>
</body>
</html>
script.js:
// Creates an alert, when a specific button is pressed
function birthday() {
alert('September 9th');
}
// Creates an alert, when the body is loaded
function loading() {
alert("Page loaded successfully!");
}
function myFunction() {
var hour = new Date().getHours();
var greeting;
if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
alert(myFunction);
}
Aucun commentaire:
Enregistrer un commentaire