I have a search bar in my page. If I search something, My resultdiv shows me result. But when I move to next page and come back on clicking back button the resultdiv don't show my results. I want the resultdiv to be preserved. I use keyup event on my search bar.
Asked
Active
Viewed 513 times
0
-
yes, iam using PHP with jquery – Kalidas Rajeev Jun 24 '19 at 06:55
-
you could start using the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History) `replaceState` to keep the last searched term in the URL. After navigating back you can fetch the data from the url... alternatively store the information in [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) – Tobias Krogh Jun 24 '19 at 07:00
-
you are using JQuery AJAX to load search results? OR are you talking about browser's search bar? – Developer Jun 24 '19 at 07:04
-
I am loading my data from ajax – Kalidas Rajeev Jun 24 '19 at 07:06
-
I have no experience with history api – Kalidas Rajeev Jun 24 '19 at 07:07
-
If I'm not wrong then you must be binding AJAX response to result div, so you can use different DIV for search result list and result clicked after search item. You will have to show and hide the respective DIV on search item click and back button click to achieve this – Developer Jun 24 '19 at 07:15
1 Answers
1
If you apply your search in the url bar with a question mark (known as $_GET
in PHP) you can use the JavaScript method window.history.go( -1 )
and this goes back to the same path before which includes the $_GET
variable.
URL bar:
my_page.php?search=something

Alireza A2F
- 519
- 4
- 26
-
-
OP is using AJAX to load data so it will not helpful if he is loading results using ajax and binding same to result div – Developer Jun 24 '19 at 07:07
-
I want my result from keyup event to be preserved. I really need your help to tackle this. – Kalidas Rajeev Jun 24 '19 at 07:12
-
@KalidasRajeev maybe this will be helpful: https://stackoverflow.com/questions/1462719/javascript-change-the-function-of-the-browsers-back-button – Alireza A2F Jun 24 '19 at 07:14