# Returning to a search via back

**URL:** <https://meta.discourse.org/t/returning-to-a-search-via-back/61503>\
**Category:** Bug\
**Created:** [2017年四月24日 16:08 UTC](https://meta.discourse.org/t/returning-to-a-search-via-back/61503 "2017-04-24T16:08:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JagWaugh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jagwaugh/32/69335_2.png) [@JagWaugh](https://meta.discourse.org/u/JagWaugh)\
**Post date:** [2017年四月24日 16:08 UTC](https://meta.discourse.org/t/returning-to-a-search-via-back/61503/1 "2017-04-24T16:08:11Z")

</div>

A user asked about this today.

If you’ve used search to get a list of posts, scroll down then click on one of them, if you then use the back function in the browser it always returns you to the top of the search results.

If you view a category, scroll down, then open a topic, the back button returns you to your previous scrolling position.

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2017年四月26日 19:09 UTC](https://meta.discourse.org/t/returning-to-a-search-via-back/61503/2 "2017-04-26T19:09:02Z")

</div>

I believe this was reported before but can not find the topic or discussion fragment, its a bug we should fix.

---

<div class="post-metadata">

**Author:** ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)\
**Post date:** [2017年四月27日 01:46 UTC](https://meta.discourse.org/t/returning-to-a-search-via-back/61503/3 "2017-04-27T01:46:08Z")

</div>

Sure if this is not too hard or risky maybe @eviltrout could take it.

---

<div class="post-metadata">

**Author:** ![nbianca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nbianca/32/157984_2.png) [@nbianca](https://meta.discourse.org/u/nbianca)\
**Post date:** [2017年八月10日 20:54 UTC](https://meta.discourse.org/t/returning-to-a-search-via-back/61503/5 "2017-08-10T20:54:50Z")

</div>

Hey,

I have been trying to fix this issue. I implemented it like this so far: [https://github.com/discourse/discourse/compare/master...nbianca:scroll\_tracking?expand=1](https://github.com/discourse/discourse/compare/master...nbianca:scroll_tracking?expand=1)

The issue is that even though `scrollTo` has the right value in `didInsertElement`, the `scrollTop` method does not seem to work (probably rendering has not finished?).

Can anyone take a look, please?

---

<div class="post-metadata">

**Author:** ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)\
**Post date:** [2017年八月11日 08:24 UTC](https://meta.discourse.org/t/returning-to-a-search-via-back/61503/6 "2017-08-11T08:24:49Z")

</div>

Hi Bianca,

I would implement it this way:

```plaintext
import Scrolling from "discourse/mixins/scrolling";

export default Ember.Component.extend(Scrolling, {
  didInsertElement() {
    this._super();

    this.bindScrolling({ name: this.get("name") });
  },

  didRender() {
    this._super();

    const scrollToY = this.session.get(this.get("trackerName"));
    if (scrollToY && scrollToY >= 0) {
      Ember.run.next(() => $(window).scrollTop(scrollToY + 1));
    }
  },

  didReceiveAttrs() {
    this._super();

    this.set("trackerName", `scroll-tracker-${this.get("name")}`);
  },

  willDestroyElement() {
    this._super();

    this.unbindScrolling(this.get("name"));
  },

  scrolled() {
    this._super();

    this.session.set(this.get("trackerName"), $(window).scrollTop());
  },
});

```

It should work, feel free to modify the coding style. Thanks for your contribution.

---

<div class="post-metadata">

**Author:** ![nbianca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nbianca/32/157984_2.png) [@nbianca](https://meta.discourse.org/u/nbianca)\
**Post date:** [2017年八月12日 20:07 UTC](https://meta.discourse.org/t/returning-to-a-search-via-back/61503/7 "2017-08-12T20:07:03Z")

</div>

Thank you a lot! 🎊 That fixed my issue and made my code look a lot better.

I submitted this pull request: [Scroll is tracked on search page. by nbianca · Pull Request #5040 · discourse/discourse · GitHub](https://github.com/discourse/discourse/pull/5040)

---

<div class="post-metadata">

**Author:** ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)\
**Post date:** [2017年八月13日 13:21 UTC](https://meta.discourse.org/t/returning-to-a-search-via-back/61503/8 "2017-08-13T13:21:25Z")

</div>

Merged, thanks Bianca.
