1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'HTML & JavaScript' started by Repulsor, May 20, 2015.

  1. Repulsor

    Repulsor Power Member

    Joined:
    Jun 11, 2013
    Messages:
    773
    Likes Received:
    280
    Location:
    PHP Scripting ;)
    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 :)
     
    • Thanks Thanks x 13
  2. peetrike

    peetrike Power Member

    Joined:
    Aug 19, 2012
    Messages:
    585
    Likes Received:
    219
    Location:
    Estonia
    Thanks! I´ve been looking this for ages. There are many WP plugins but not for other CMS.

    Cheers
     
  3. Repulsor

    Repulsor Power Member

    Joined:
    Jun 11, 2013
    Messages:
    773
    Likes Received:
    280
    Location:
    PHP Scripting ;)
    This should work on all CMS. I could only test in WP since I had no other CMS installations. But should work fine as long as this javascript is loaded on page load. Works on Firebox and Chrome. Let me know if you are getting any errors.
     
  4. abhi007

    abhi007 Jr. VIP Jr. VIP

    Joined:
    Aug 31, 2010
    Messages:
    5,867
    Likes Received:
    3,947
    Location:
    Theatre of dreams :)
    This should come in Handy :)
     
    • Thanks Thanks x 1
  5. Repulsor

    Repulsor Power Member

    Joined:
    Jun 11, 2013
    Messages:
    773
    Likes Received:
    280
    Location:
    PHP Scripting ;)
    Try out and do let me know if it works well for you. :) Glad I could help.
     
  6. skipperskipper

    skipperskipper Newbie

    Joined:
    Aug 26, 2014
    Messages:
    33
    Likes Received:
    8
    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/
     
    • Thanks Thanks x 1
  7. Repulsor

    Repulsor Power Member

    Joined:
    Jun 11, 2013
    Messages:
    773
    Likes Received:
    280
    Location:
    PHP Scripting ;)
    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: May 20, 2015
  8. nuhll

    nuhll Junior Member

    Joined:
    Jul 8, 2015
    Messages:
    196
    Likes Received:
    17
    does this work for websites which are in http and in https?
     
  9. akanz

    akanz Newbie

    Joined:
    Nov 8, 2014
    Messages:
    25
    Likes Received:
    0
    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.
     
  10. ghat6

    ghat6 Junior Member

    Joined:
    May 3, 2010
    Messages:
    107
    Likes Received:
    39
    very cool little script. Thanks OP!
     
  11. cottonwolf

    cottonwolf Regular Member

    Joined:
    Jan 20, 2015
    Messages:
    469
    Likes Received:
    239
    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.
     
  12. UptonGoodwin

    UptonGoodwin Power Member

    Joined:
    Jan 11, 2012
    Messages:
    593
    Likes Received:
    269
    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.
     
    • Thanks Thanks x 1
  13. ChristineAshe

    ChristineAshe Registered Member

    Joined:
    Aug 19, 2014
    Messages:
    70
    Likes Received:
    49
    Occupation:
    Perf. Marketing
    I swear i was looking for something like this for ages, and then boom... i read your post!
    Many thanks for this script.
     
  14. laurentiukendo

    laurentiukendo Junior Member

    Joined:
    Jun 26, 2012
    Messages:
    114
    Likes Received:
    19
    Gender:
    Male
    Occupation:
    Making money
    Location:
    Romania
    Awesome scripts.This is very good to get even more conversions.Thanks a lot mate
     
  15. Amazingsalomon

    Amazingsalomon Newbie

    Joined:
    Oct 17, 2015
    Messages:
    5
    Likes Received:
    0
    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 :)
     
  16. Hawkster

    Hawkster Jr. VIP Jr. VIP

    Joined:
    Jun 22, 2013
    Messages:
    3,510
    Likes Received:
    3,729
    Gender:
    Male
    Occupation:
    Listen to everyone - Follow no-one
    Location:
    UK
    Home Page:
  17. webpen

    webpen Newbie

    Joined:
    Feb 11, 2007
    Messages:
    7
    Likes Received:
    0
    Location:
    Spain
    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?
     
  18. webpen

    webpen Newbie

    Joined:
    Feb 11, 2007
    Messages:
    7
    Likes Received:
    0
    Location:
    Spain
    Sorry - now working - stupid text editor on mac!!
     
  19. Lomomu

    Lomomu Junior Member

    Joined:
    Nov 19, 2015
    Messages:
    109
    Likes Received:
    24
    Legend, thank you so much that's great!
     
  20. ryanhackmachine

    ryanhackmachine Regular Member

    Joined:
    May 8, 2010
    Messages:
    393
    Likes Received:
    224
    Occupation:
    Spend Money
    Location:
    In front of my desktop pc
    took google 2 months before they ban my website got my manual action few weeks ago