Resource
/
Change Page Title When User Switches Tabs

Details

Category
Gimmicks
Last Updated
Feb 2, 2026

Overview

This script changes the tab title when a user switches away. It briefly replaces the original title with a custom message after a short delay, syncs it across open tabs, and restores the original title once the user returns.

Setup

00

Add External Scripts

Copy & paste the scripts before the </body> tag of your project. If you added them before for another setup, skip this step.

00

Add HTML

Place the code in an HTML widget or add it through Elementor → Custom Code (before the closing </body> tag) either globally or only on selected pages.

00

Add custom CSS

Paste the code through the page or site settings, or add it via Elementor → Custom Code (before </body>) for broader use.

00

Add custom Javascript

Paste the script through Elementor → Custom Code (set to load after </body>) for site-wide or page-specific loading.

(function () {
  const originalTitle = document.title;
  const awayMessage = "👀 Oh… so we’re done?"; // title when tab is inactive

  function handleVisibility() {
    document.title = document.hidden ? awayMessage : originalTitle;
  }

  document.addEventListener("visibilitychange", handleVisibility);
})();
00

Add custom PHP

Place the PHP snippet inside your theme’s functions.php or using any code snippet to enable logic.

00

Publish and preview live

Some solutions only work on the live site. Always publish and test after each change, as results may not appear in the editor.

You can customize this snippet by changing the awayMessage value. This controls the exact text or emoji shown when the tab becomes inactive.

To limit the message to a specific page, paste the code into an HTML widget and adjust the message only for that page.

For broader control, use Elementor’s Custom Code feature or a WordPress code snippet plugin. This lets you enable or disable the feature on selected pages or page types.