Back button to previous page (unless it's a different domain)

Thank you for the answers and sorry for not replying. This has been on the back burner for a while. I’ve managed to make it work by doing the following;

Turn goBack attribute to onClick event (since Webflow doesn’t allow it) using the following code in the head of the page:

<script>
        window.onload = function() {
            var anchors = document.getElementsByTagName('*');
            for(var i = 0; i < anchors.length; i++) {
                var anchor = anchors[i];
                anchor.onclick = function() {
                    code = this.getAttribute('goBack');
                    eval(code);   
                }
            }
        }
        </script>

Thanks to bonavnon (Add onClick attribute to any element with this JS snippet)

Add custom attribute:
Screenshot 2020-01-17 at 18.01.25

Add following code to the body of the page:

<script>
    function backClick() {
    if(document.referrer.indexOf('yourdomain.com') >= 0) {
        history.go(-1);
    }
    else {
        window.location.href = '/'; // this might just be '/' of your site
    }
    return false;
        }
    </script>

This will create a browser back button, unless the user was not on your domain and will then be redirected to the home page.

Hope this helps anyone else!

1 Like