Streaming live at 10am (PST)

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

I am trying to create a back button using JS with a condition in case the user comes from any domain other than my website.

To put this in context:

  1. User browses from category to CMS item, the back button will send the user to category.
  2. the user browses from home page to the same CMS item, the back button will send the user to home page
  3. Condition is to set a default page for the back button if the user comes from any other domain (for example from Google to CMS item, the user is sent to home page).

I’ve found the follow code:

function backClick() {
			if (document.referrer.indexOf( !== -1) {
				history.go(-1); return false;
			else { window.location.href = ''; }
<a onclick="backClick()">

To set the onclick event attribute in Webflow, I’ve found this solution: Add onClick attribute to any element with this JS snippet

I’ve placed both the code to rename the onclick attribute (according to forum post) together with the function backClick in the body of the page, but no luck.

Can anyone guide me to where/how to implement this code?

1 Like

There’s a lot of things that could be going wrong, can you share the project?

This is what i’m using!

//***On button***//

//***General Script***//
function closebutton(){if(history.length===1){window.location=""}else{history.back()}}

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:

        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');

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:

    function backClick() {
    if(document.referrer.indexOf('') >= 0) {
    else {
        window.location.href = '/'; // this might just be '/' of your site
    return false;

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!

A lot simpler.
For small projects or single page sites :crazy_face:
<a onclick="history.go(-1); return false;">Get Back Loretta</a>