In This Article:
An eye-catching flashing tab title serves as an entertaining and efficient method to capture your visitors' attention when they leave your site. By alternating between two messages in the browser tab, you can prompt users to return, showcase special offers, or simply infuse a creative touch into your website. In this tutorial, you will discover how to create the flashing tab title effect and implement it on a specific page of your site. Let’s get started!
Flashing tab titles are a simple way to engage users when your website is not in focus. They can be used to:
But you may not want this feature across your entire website. Sometimes, it’s best to limit it to a specific page, like a landing page or promotion page.
Here’s what will happen:
First, create the webpage where you want the effect to appear. We are using just this blog post for this feature.
To make this effect work on just one page, we’ll use JavaScript to check the page’s URL or unique identifier.
Option 1: Check the Page URL
Since your website has unique URLs for each page, you can use this script to target specific page URLS:
html
<script>
// Check if the current page is the one you want the effect on
if (window.location.pathname === "/flashing-tab-page") {
const originalTitle = document.title;
const flashTitles = ["Come Back", "To My Blog Post"];
let flashIndex = 0;
let flashInterval;
function startFlashing() {
flashInterval = setInterval(() => {
document.title = flashTitles[flashIndex];
flashIndex = (flashIndex + 1) % flashTitles.length;
}, 1500);
}
function stopFlashing() {
clearInterval(flashInterval);
document.title = originalTitle;
}
document.addEventListener("visibilitychange", function () {
if (document.hidden) {
startFlashing();
} else {
stopFlashing();
}
});
}
</script>
In this example:
window.location.pathname: Checks the page's URL path (e.g., /flashing-tab-page).
The script runs only if the user is on the page /flashing-tab-page.
Option 2: Body End for sitewide Effect
Sitewide. You can place this script in the Body End of your Duda website (either with your SEO & Settings in the left context bar, or in Dev Mode). Simply edit the code with your effect (your messages, and time) and place it in the body end file on your Duda Website.
Option 3: Single Page without URL recognition.
The Following script will work in either the Body-End (option 2), or in an HTML Widget to apply it to just a single page!
<script>
// Store the original title
const originalTitle = document.title;
// Titles to flash
const flashTitles = ["Come Back", "To My Blog Post"];
let flashIndex = 0; // Index to track the current flashing title
let flashInterval; // To store the interval ID
// Function to start flashing titles
function startFlashing() {
flashInterval = setInterval(() => {
document.title = flashTitles[flashIndex];
flashIndex = (flashIndex + 1) % flashTitles.length; // Toggle between titles
}, 1500); // Change every 1.5 seconds
}
// Function to stop flashing and restore the original title
function stopFlashing() {
clearInterval(flashInterval); // Stop the interval
document.title = originalTitle; // Restore the original title
}
// Listen for visibility change
document.addEventListener("visibilitychange", function () {
if (document.hidden) {
startFlashing(); // Start flashing when the tab is inactive
} else {
stopFlashing(); // Stop flashing when the tab is active
}
});
</script>
Save and open the site > Switch to another tab > Watch the tab title alternate between “Come Back” and “To Cre8 Widgets” every 1.5 seconds.
Return to the tab, and the original title will be restored.
You can easily tweak the script to fit your needs:
While a flashing tab title can be a useful attention-grabber, it might not make sense across your entire website. Limiting it to a single page ensures:
By targeting a specific page with a flashing tab title effect, you can effectively grab your visitors’ attention without overusing this feature. Whether you’re using a unique URL or a page-specific identifier, this guide gives you all the tools you need to implement it seamlessly.
Now it’s your turn! Add this effect to your site, and let your creativity shine. If you’ve implemented this, share your experience in the comments below. Happy coding! 🚀
WIDGETS BY CRE8 MY SITE