# Possible to check previous page user visited?

**URL:** <https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988>\
**Category:** support\
**Created:** [May 31, 2019, 4:42pm UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988 "2019-05-31T16:42:11Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobby](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nobby](https://discourse.gohugo.io/u/nobby)\
**Post date:** [May 31, 2019, 4:42pm UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/1 "2019-05-31T16:42:12Z")

</div>

How do I figure out what page a user previously was on before landing on a specific page?

As an example, when a user visits `products/product-a/` and clicks on `preview`, they come to the `products/preview` page. I’d like to display a link to the `products/product-a/` page on `products/preview`. If they were previously on `products/product-b`/, I’d like to show that link.

Content

```
 products
   _index.md
   product-a.md
   product-b.md
   preview
     index.md [layout = preview]

```

Layout

```
layout > products > single.html
layout > products > preview.html
```

---

<div class="post-metadata">

**Author:** ![nobby](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nobby](https://discourse.gohugo.io/u/nobby)\
**Post date:** [June 1, 2019, 1:28am UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/2 "2019-06-01T01:28:01Z")

</div>

It seems I can do this using the JavaScript function `document.referrer;` which gives the previous page.

Is it possible to pass the javascript variable to a go variable?

Something like this:

```
<script>
var url = "{{$url := slice}}
var url = document.referrer;
var url += "{{$url}}"
</script>

```

Calling {{$url}} gives document.referrer;

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [June 1, 2019, 1:51am UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/3 "2019-06-01T01:51:50Z")

</div>

You can build JavaScript with Hugo templating, but not the other way around.

---

<div class="post-metadata">

**Author:** ![rdwatters](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rdwatters/32/2848_2.png) [@rdwatters](https://discourse.gohugo.io/u/rdwatters)\
**Post date:** [June 1, 2019, 1:55am UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/4 "2019-06-01T01:55:00Z")

</div>

Agreed with @zwbetz. From what I can tell you don’t actually need Hugo for anything you want to do…

---

<div class="post-metadata">

**Author:** ![nobby](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nobby](https://discourse.gohugo.io/u/nobby)\
**Post date:** [June 1, 2019, 2:04am UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/5 "2019-06-01T02:04:10Z")

</div>

Thanks. I’m not familiar with javascript so that’s why I was wondering if it was possible. I need to think a bit more about what I’m trying to achieve as I’m overcomplicating things. Thanks again.

---

<div class="post-metadata">

**Author:** ![RickCogley](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rickcogley/32/10269_2.png) [@RickCogley](https://discourse.gohugo.io/u/RickCogley)\
**Post date:** [June 1, 2019, 6:05am UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/6 "2019-06-01T06:05:00Z")

</div>

Most css libraries achieve this with “breadcrumbs” right?

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [June 1, 2019, 8:32am UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/7 "2019-06-01T08:32:54Z")

</div>

If you DON’T want to know which page they were on, but WANT to send them to the last page they were on (semantics, sorry) use the following:

```auto
<a href="javascript:history.back();">Back to last page</a>
<a href="javascript:history.back(2);">Back to page before last page</a>

```

---

<div class="post-metadata">

**Author:** ![nobby](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nobby](https://discourse.gohugo.io/u/nobby)\
**Post date:** [June 1, 2019, 2:12pm UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/8 "2019-06-01T14:12:09Z")

</div>

Thanks all for the suggestions.

So what I’m trying to do is the following:

- I want to figure out the previous page a user was on in order to display information related to that page on the current page they are viewing.

Example:

- User visits `products/product-a/`

- User clicks on the preview link

- User lands on `products/preview/`

- On `products/preview/` the previous page’s – `products/product-a/` – title and description along with other information from the markdown file, `product-a.md`, is displayed.

I thought by using `document.referrer`; I would be apply to capture the `url` of the previous page, pass it to a go variable, and then do the logic using Hugo. But that’s apparently not possible.

My understanding of breadcrumbs is that they follow the structure of the content folder. Using my example:

When a user visits `products/products-a/`, the breadcrumbs become:

`home > products > products-a`

When a user visits preview from `products/product-a/`, the breadcrumbs become:

`home > products > preview.`

Any suggestions on this?

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [June 1, 2019, 2:19pm UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/9 "2019-06-01T14:19:29Z")

</div>

if the breadcrumb is `home > products > preview` you don’ know he was on `products-a` before so it’s not a referrer thing but a simple hierarchical thing of the current page. because `preview.md` is (if you did it all the way it works best) inside of `/content/products/`.

/content/products/products-a.md  
/content/products/preview.md

any breadcrumb template will display it right if your files are located like these.

The problem is: I have the quaint feeling that yo would probably prefer

home \> products \> product-a  
home \> products \> product-a \> preview

from a logical point of view. in this case you need a subfolder `preview` in your `product-a` folder.

---

<div class="post-metadata">

**Author:** ![nobby](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nobby](https://discourse.gohugo.io/u/nobby)\
**Post date:** [June 1, 2019, 2:21pm UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/10 "2019-06-01T14:21:45Z")

</div>

You’re correct. This would mean creating a subfolder in each product folder which number in the hundreds which isn’t optimal.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [June 1, 2019, 2:24pm UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/11 "2019-06-01T14:24:25Z")

</div>

In that case: RETHINK.

You want a preview of something. What is the preview? A gallery? Add a gallery to product-a.md that pop ups per click. A file sample (PDF?)? add it as page ressource to product a.

Page ressources:  
/content/products/product-a/index.md - product info (the index.html page)  
/content/products/product-a/sample.pdf - a page resource you can use in index.html

Check out page resources in the docs.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [June 1, 2019, 2:25pm UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/12 "2019-06-01T14:25:20Z")

</div>

> **[Page Bundles](https://gohugo.io/content-management/page-bundles/)**
>
> Content organization using Page Bundles

> **[Page Resources](https://gohugo.io/content-management/page-resources/)**
>
> Page Resources -- images, other pages, documents etc. -- have page-relative URLs and their own metadata.

---

<div class="post-metadata">

**Author:** ![nobby](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nobby](https://discourse.gohugo.io/u/nobby)\
**Post date:** [June 1, 2019, 2:25pm UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/13 "2019-06-01T14:25:26Z")

</div>

Thank you! Will do.

---

<div class="post-metadata">

**Author:** ![RickCogley](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rickcogley/32/10269_2.png) [@RickCogley](https://discourse.gohugo.io/u/RickCogley)\
**Post date:** [June 1, 2019, 10:10pm UTC](https://discourse.gohugo.io/t/possible-to-check-previous-page-user-visited/18988/14 "2019-06-01T22:10:52Z")

</div>

An important thing to note is, hugo is building before your users view the site, so, it’s not involved. Nor does it inject any javascript automatically. It helps you assemble the site, taking exactly what you give it and building the site quickly.
