Explain Codes LogoExplain Codes Logo

How to dynamically change a webpage's title?

javascript
dynamic-title
seo
performance
Nikita BarsukovbyNikita Barsukov·Aug 16, 2024
TLDR

Effortlessly update a webpage title using JavaScript. Assign a new string to document.title:

document.title = "Brand New Title";

This immediate update won't reload your page, which perfectly blends with your dynamic single-page applications (SPA). Let's dive deeper!

Putting the 'dynamic' in webpages

Dynamic title changes can spark a visually appealing impact on user engagement and traffic in the combination of search engine optimization (SEO). Modern search engines relish the taste of JavaScript-altered contents, say, a document.title on your webpage. That said, crafting a relevant and descriptive title can potentially elevate your webpage's luck in the SEO battlefield.

Titles on tabs: Reflect thy content

Hooked your users with your SPA? Keep them around longer by matching your title with the currently viewed content. It's like giving your users a content compass!

function updateTitle(activeTab) { // Even titles get bored staring at the same content! document.title = activeTab; }

SEO insights: Debunk your server-side myths

Googlebot and its cousins are evolving – they're learning JavaScript. Ensure your masterfully crafted JavaScript and your make-or-break CSS files shimmer in the robots.txt limelight. Your webpage's SEO reputation depends on it!

Trimming down to performance

Declutter your webpage’s content, focusing on nothing but the crème de la crème. Shed irrelevant scripts and bulky media – everything slowing down your much desired high performance.

Theatrical presentation

Picture dynamically changing your webpage's title as a theatre marquee sign change:

Old Marquee: 🎭 "The Phantom of the Opera"
document.title = '🎊 "The Grand Finale"';
New Marquee: 🎊 "The Grand Finale"

And voila, your webpage's title now majestically announces what's happening in your SPA – like showcasing the “next big show!”

Dynamic title enhancements: Exploring other avenues

Wheeling in the HTML5 History API

Think viewing a webpage as a memorable trip. Every page visit is a stop, and every title change marks a new highlight of the tour.

history.pushState({}, 'The State Museum', '/museum'); // Just stopped by the museum. Don't forget to change the tour guide banner! document.title = 'The State Museum';

Wearing the meta tag hat

Give your document.title a best friend – meta[name="description"]. They work in harmony to present your webpage's unique identity to your users.

document.querySelector('meta[name="description"]').setAttribute("content", "The State Museum Description"); // Because even descriptions love some change!

Server-side rendering vs. Client-side wizardry

Remember, server-side and client-side rendering are two sides of the same coin. Just because JavaScript can manipulate titles doesn’t mean we totally ditch server-side rendering. You never know when an old-school search engine might drop by!

Tools to grab: Making life easier

Grease your JavaScript rendering with Prerender.io. Let none of your performance optimizations go unnoticed. Wondering how visible your webpage is to crawlers? Google has you covered with "Fetch as Google".

Watch steps: Precautions and learnings

Whilst JavaScript is marvelously powerful, it demands moderation. Make sure you balance your JavaScript usage with your webpage's accessibility, SEO, and most importantly, your precious users' experience.