If you are looking for a simple JavaScript function that you can include in your HTML opener, here's an example that shows how to create a button that, when clicked, displays an alert message.
Below is the updated HTML code with a JavaScript function added:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to My Web Page</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 20px;
text-align: center;
}
h1 {
color: #333;
}
p {
color: #666;
}
a {
color: #1e90ff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
/* Add some basic styling for responsiveness */
@media (max-width: 600px) {
h1 {
font-size: 1.5em;
}
p {
font-size: 1em;
}
}
</style>
<script>
function showAlert() {
alert("Welcome to my web page! Enjoy your stay!");
}
</script>
</head>
<body>
<h1>Welcome to My Web Page!</h1>
<p>This is a simple HTML opener page to showcase the structure of an HTML document.</p>
<p>To learn more about web development, check out <a href="https://www.w3schools.com/">W3Schools</a>.</p>
<!-- Button to trigger the JavaScript function -->
<button onclick="showAlert()">Click Me!</button>
</body>
</html>
Explanation of the JavaScript Function
-
Function Declaration:
- The JavaScript function
showAlert() is defined in the <script> tag. This function uses the alert method to show a popup message when called.
-
Button Element:
- A
<button> element is added to the HTML body. The onclick attribute is set to call the showAlert() function when the button is clicked.
How to Use This Code
- Copy the entire updated HTML code above.
- Open a text editor.
- Paste the code into the text editor.
- Save the file with a
.html extension (e.g., opener.html).
- Open the saved file in a web browser.
- Click the "Click Me!" button to see the alert message.
Feel free to modify the alert message or the styling of the button to fit your needs!