That has been a practice widely used by “SEO specialists” to retain users for more time within their websites. The trend led to a large number of WordPress plugins in the sidelines, which promise to “reduce bounce rates” hijacking the back button in the user’s browser, and redirecting it to any page they want.

First and foremost – as usual – website owners are paying huge amounts to get something that is technically pretty trivial. And, more important: they are exposing their websites to possible sanctions from Google and other search engines in the near future.

How can one hijack the browser button?

The answer is short: Javascript. And the code itself is so simple that will make those paying 50, 100 or even 200 bucks for a plugin to evaluate their own business models. Of course there are hundreds of ways to hijack the browser back button using JS – but this is probably the simplest:

window.history.pushState({page: 1}, "", "");

window.onpopstate = function(event) {
    if(event){
        window.location.href = 'https://www.google.com/';
        // Code to handle back button or prevent from navigation
    }
}

The code above, added to a page HEAD section, does the trick. If the user clicks back in the browser, he will be automatically redirected to Google Search’s main page. End story. Put a variable and design a simple PHP wrapper and done: you have a plugin.

For instance: let’s say we use ACF plugin to put up a new field named “redir” in all posts. That makes possible to set a different URL for each new post. And then, for a plugin, all we need to do is to use the hooks template_redirect and wp_head to embed the JS code in each post, using the URL saved in the ACF meta field.

add_action('template_redirect', 'redir');

function redir() {
	global $post;	
	$url = get_field('redir'); // Previously created ACF field
	if($url) {
// Hook an anonymous function to wp_head, just if the ACF field has an entry
		add_action('wp_head', function () use ($url) {
			// Javascript code with $url variable as window.location.href
		});
	}
}

Presto! You have a functional plugin to manage that, or can even add the code in the theme’s functions.php file. But… wait!

Why you shouldn’t do it

A simple trick that enlarges your user retention with no hassle. Brilliant, huh? A problem: if it is so simple to put up, how long do you think Google will be spending to sort this out? The fact is that Google is already working at a patch for Chrome, as a way to convey the SEO black hat practice.

The price for a couple of months of good metrics can be heavy. Google’s business model depends on the reliability of their ads and metrics – so you can’t expect to be forgotten. And, if Chrome can deal with that, the rest of the browsers should be doing the same in no time.

Hijacking the browser back button is something that, just by the terminology “hijack”, is definitely not a good practice. Think twice…

One reply on “How to easily hijack the browser back button and why you shouldn’t do it”

Comments are closed.