To achieve the functionality where clicking any link pointing to domain.com/#soon shows a "Coming Soon" notice, you can use JavaScript to detect the click event on such links and then display the notice. Below is a simple JavaScript code snippet that accomplishes this task. This script assumes you're checking links that have domain.com/#soon or just #soon in their href attribute.
document.addEventListener('DOMContentLoaded', function() {
// Function to check if a link points to #soon
function checkLink(href) {
return href.includes('#soon');
}
// Function to show the notice
function showNotice() {
alert('Coming Soon!');
// You can replace the alert with a more sophisticated notice display method
// For example, showing a div with the notice
}
// Attach event listener to all links
document.addEventListener('click', function(event) {
if (event.target.tagName === 'A') {
var link = event.target;
// Check if the link is pointing to #soon
if (checkLink(link.href)) {
event.preventDefault(); // Prevent the default link behavior
showNotice(); // Show the coming soon notice
}
}
});
});
This script works as follows:
- It waits for the DOM to be fully loaded (
DOMContentLoaded event).
- It defines a function
checkLink to check if a link's href attribute contains #soon.
- It defines a function
showNotice to display the "Coming Soon" notice. Currently, it uses an alert, but you can replace this with a more user-friendly method, like displaying a hidden <div> with the notice.
- It attaches a click event listener to the
document. When a click occurs, it checks if the clicked element is an <a> tag.
- If it's an
<a> tag and its href contains #soon, it prevents the default link behavior (event.preventDefault()) and calls showNotice() to display the "Coming Soon" notice.
Alternative: Displaying a Notice in a <div>
If you prefer to display the notice in a more controlled manner, you can add a <div> to your HTML:
<div id="comingSoonNotice" style="display:none;">Coming Soon!</div>
And modify the showNotice function to display this <div>:
function showNotice() {
document.getElementById('comingSoonNotice').style.display = 'block';
// Optionally, you can add a timer to hide it after a few seconds
setTimeout(function() {
document.getElementById('comingSoonNotice').style.display = 'none';
}, 3000); // Hides after 3 seconds
}
You might need to add some CSS to make the #comingSoonNotice <div> more visible, for example:
#comingSoonNotice {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #fff;
padding: 20px;
border: 1px solid #ddd;
box-shadow: 0px 0px 10px rgba(0,0,0,0.2);
}