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

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

<p>You're welcome to play with calling JavaScript method for the cancel buttons.</p>

CC BY-SA 3.0
2017-10-07T19:12:47.147Z
Excellent. I see what you mean now... Thank you dominik :)
ToddPadwick
CC BY-SA 3.0
2017-10-07T18:09:19.707Z
2017-10-07T18:09:19.707Z
Dominik Krulak
<p>You could use the <a href="https://github.com/nystudio107/cookies" rel="nofollow noreferrer">Cookies</a> 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:</p>

<pre><code>{{ setCookie( 'lastUrlBeforeStepStuff', craft.request.url) }}
</code></pre>

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

<pre><code>&lt;a href="{{ getCookie( 'lastUrlBeforeStepStuff' )|default('/some/path/to/fall/back/to') }}"&gt;Cancel&lt;/a&gt;
</code></pre>

CC BY-SA 3.0
2017-10-09T10:54:26.527Z
I recommend this over the currently accepted answer, which will fail if you open the page in a new tab, as an example.
Fred Carlsen
CC BY-SA 3.0
2017-10-07T23:21:02.963Z
2017-10-08T20:10:58.113Z
2017-10-08T20:10:58.113Z
Mats Mikkel Rummelhoff
<p>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.</p>

<p>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?</p>

<p>see user journey diagram:</p>

<p><a href="https://i.sstatic.net/sMZXH.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/sMZXH.jpg" alt="enter image description here"></a></p>

CC BY-SA 3.0
2017-10-06T19:12:52.550Z
According to your visual a simple [go back button for browser history](https://www.w3schools.com/jsref/met_his_back.asp) (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.
Dominik Krulak
CC BY-SA 3.0
2017-10-06T19:15:07.863Z
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?
ToddPadwick
CC BY-SA 3.0
2017-10-06T19:22:42.480Z
Well how would you call the correct link? Only browser history knows that.
Dominik Krulak
CC BY-SA 3.0
2017-10-06T19:24:53.940Z
I don't know, that's why I'm asking. Im pretty novice with all of this. Maybe cookies?
ToddPadwick
CC BY-SA 3.0
2017-10-06T19:32:30.157Z
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.
Dominik Krulak
CC BY-SA 3.0
2017-10-07T14:53:50.670Z
But what if they came straight to step two without visiting step one? This is the issue. How would i know whether they  had been to step one or not? If they haven't the browser needs to only go back one.
ToddPadwick
<templating><url><redirect><pagination>
provide a 'back' or 'cancel' or 'close' button to return to whatever page that particular template was accessed from
# provide a 'back' or 'cancel' or 'close' button to return to whatever page that particular template was accessed from

(Asked by: username_0 on 2017-10-05)

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](https://i.sstatic.net/sMZXH.jpg)](https://i.sstatic.net/sMZXH.jpg)

**Tags:** templating, url, redirect, pagination
> **Comments:**
> **username_1**: According to your visual a simple [go back button for browser history](https://www.w3schools.com/jsref/met_his_back.asp) (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.  
> **username_0**: 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?  
> **username_1**: Well how would you call the correct link? Only browser history knows that.  
> **username_0**: I don't know, that's why I'm asking. Im pretty novice with all of this. Maybe cookies?  
> **username_1**: 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.  
> **username_0**: But what if they came straight to step two without visiting step one? This is the issue. How would i know whether they  had been to step one or not? If they haven't the browser needs to only go back one.  

---

## 2 answers

### Answer by username_2 on 2017-10-07. Score: 4 | Accepted answer

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:
Cancel

> **Comments:**
> **username_3**: I recommend this over the currently accepted answer, which will fail if you open the page in a new tab, as an example.  

---

### Answer by username_1 on 2017-10-07. Score: 3

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
  Cancel
{% else %}
  // Going to STEP1
  Cancel
  // Going from STEP1 next to STEP2
  Cancel
{% endif %}

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

> **Comments:**
> **username_0**: Excellent. I see what you mean now... Thank you dominik :)  

---

