I have two templates. For the purpose of this question lets say the first template is step 1, and the second is step 2. There are links to launch these templates all over the site, but some times an entry is defined and sometimes its not. if the entry is defined, step 1 is skipped. On both steps I have a 'close/cancel' button. I need these buttons to go to whatever url the user was on when they 'launched' this process.

Is there a way of storing a URL as a variable when the 'launch' button is clicked even after moving from step 1 to step 2?

see user journey diagram:

enter image description here

  • According to your visual a simple go back button for browser history (cancel button) would work. In step two you would have to go 2 pages back. All depends how you use it. Otherwise some logic of routing would be needed. Commented Oct 6, 2017 at 19:12
  • Yes i need to avoid them going back to pages. Needs to feel like they are closing the entire experience back to where they were before step 1. So how could i store the url they were on when they launched step 1? Commented Oct 6, 2017 at 19:15
  • Well how would you call the correct link? Only browser history knows that. Commented Oct 6, 2017 at 19:22
  • I don't know, that's why I'm asking. Im pretty novice with all of this. Maybe cookies? Commented Oct 6, 2017 at 19:24
  • If you have two steps and you need to give a correct link on the cancel buttons than browser history is your friend here. As I suggested in my first comment here: If user gets to the Step 1: window.history.go(-1);. If user gets to the Step 2: window.history.go(-2);. That way a link that the user came from will be correct. Commented Oct 6, 2017 at 19:32

2 Answers 2


You could use the Cookies plugin to store the last visited URL before the user hits one of the "step" templates, i.e. by putting something like the following in every template where the links to the "steps" template occurs:

{{ setCookie( 'lastUrlBeforeStepStuff', craft.request.url) }}

Then, inside your "step" templates, you could render the "cancel" links like this:

<a href="{{ getCookie( 'lastUrlBeforeStepStuff' )|default('/some/path/to/fall/back/to') }}">Cancel</a>
  • 1
    I recommend this over the currently accepted answer, which will fail if you open the page in a new tab, as an example. Commented Oct 9, 2017 at 10:54

If you need to set a correct link on the cancel buttons than using browser history is the easiest way.

{% if entry is defined %}
  // Going to STEP2
  <button onclick="window.history.go(-1); return false;">Cancel</button>
{% else %}
  // Going to STEP1
  <button onclick="window.history.go(-1); return false;">Cancel</button>
  // Going from STEP1 next to STEP2
  <button onclick="window.history.go(-2); return false;">Cancel</button>
{% endif %}

You're welcome to play with calling JavaScript method for the cancel buttons.

  • Excellent. I see what you mean now... Thank you dominik :) Commented Oct 7, 2017 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.