Explain Codes LogoExplain Codes Logo

How to make an HTML back link?

html
back-link
tooltip
browser-compatibility
Nikita BarsukovbyNikita Barsukov·Aug 21, 2024
TLDR

To make a back link, use the <a> HTML tag with JavaScript's history.back() function inside the href attribute. For instance, <a href="javascript:history.back()">Go Back</a>.

A back link acts as a navigational compass in HTML, allowing users to retrace their steps back to the previous webpage.

Harnessing the history.back()

The history object in JavaScript stores the user's journey across web pages. The history.back() method acts like an "undo" command, propelling the user back to their last viewed page.

The role of document.referrer

We can also use document.referrer to fetch the URL of the page that led to the current page. This allows you to dynamically tailor the href attribute of your anchor tag, even though document.referrer can sometimes be fickle across different browsers due to varying implementations.

URL showcase on hover

Setting the href value with document.referrer or a javascript: URI permits the default browser behavior of displaying the URL when the mouse hovers over the link, pleasantly aligning with users' expectations.

Advanced techniques and precautions

Bolster the back link's effectiveness by incorporating additional HTML attributes and JavaScript event listeners.

Tooltip extravaganza

By using onmouseover and onmouseout events, we can morph the back link into a dynamic, interactive element that displays the previous page's URL in a custom tooltip when the mouse hovers over it.

While deploying onclick JavaScript functions, remember to include event.preventDefault() to preserve the sanctity of the default navigation action, preventing garbled history entries and a confused user.

A script that dynamically generates a back link helps in maintaining the standard hyperlink behavior, allowing for seamless interactions with other browser features or extensions.

Browser compatibility checks

For usability's sake, always test your back link solution across various browsers. Unpredictable discrepancies due to browser-specific quirks could lead to uncalled-for user discomfort.

Practical implementation and examples

Here's how you can bask in the glory of a well-crafted back link seasoned with rich functionality.

<!-- The old-school back link – simpler than a bread sandwich --> <a href="javascript:history.back()">Go Back</a>
<script> // Dynamic linking: the equivalent of changing hats in a hat shop var backLink = document.getElementById('back-link'); if(document.referrer) { backLink.setAttribute('href', document.referrer); } backLink.addEventListener('click', function(event) { event.preventDefault(); history.back(); }); </script> <a id="back-link" href="#">Go Back</a>

Unleashing tooltips to enhance the user experience

<!-- Tooltip wizardry courtesy of the HTML Hogwarts --> <a href="#" onmouseover="this.textContent=document.referrer" onmouseout="this.textContent='Go Back'" onclick="history.go(-1); return false;">Go Back</a>

Holy Grail of Control: Simulating a Back Button

In highly interactive web applications, developers might need more control over users' navigation. Enter the custom back link!

<script> // Back in action – like the Terminator function goBack() { history.back(); } </script> <button onclick="goBack()">Go Back</button>

We're turning an ordinary button into a site-specific back button using the history object.

Keeping Browser History Intact

Preventing side-effects while keeping navigation stack go(-1) clean.

<!-- Shipping integrity with your back link – fragile content inside! --> <a href="#" onclick="event.preventDefault(); history.back();">Go Back</a>

This method ensures no nasty surprises are waiting in the back-stack, like duplicate entries that disturb user navigation.