@@ -59,6 +59,18 @@ class SearchManager {
59
59
} ) ;
60
60
61
61
this . #initializeForm( ) ;
62
+
63
+ window . addEventListener ( 'popstate' , ( event ) => {
64
+ // Handle the navigation event (back/forward button)
65
+ if ( event . state ?. searchContent ) {
66
+ const content = event . state . searchContent ;
67
+ const existingContent = document . querySelector ( this . #options. contentSelector ) ;
68
+
69
+ if ( content && existingContent ) {
70
+ existingContent . innerHTML = content ;
71
+ }
72
+ }
73
+ } ) ;
62
74
}
63
75
64
76
#initializeForm( ) {
@@ -117,17 +129,15 @@ class SearchManager {
117
129
}
118
130
119
131
const text = await response . text ( ) ;
120
- const parser = new DOMParser ( ) ;
121
- const doc = parser . parseFromString ( text , 'text/html' ) ;
122
- const newContent = doc . querySelector ( this . #options. contentSelector ) ;
132
+ const newContent = this . parseResponseContent ( text , this . #options. contentSelector ) ;
123
133
const existingContent = document . querySelector ( this . #options. contentSelector ) ;
124
134
125
135
if ( newContent && existingContent ) {
126
136
existingContent . replaceWith ( newContent ) ;
127
137
this . #initializeForm( ) ;
128
138
}
129
139
130
- history . pushState ( null , '' , url ) ;
140
+ history . pushState ( { searchContent : newContent . innerHTML } , '' , url ) ;
131
141
} catch ( error ) {
132
142
console . error ( 'Error fetching search results:' , error ) ;
133
143
} finally {
@@ -158,6 +168,13 @@ class SearchManager {
158
168
}
159
169
}
160
170
171
+ parseResponseContent ( html , selector ) {
172
+ const parser = new DOMParser ( ) ;
173
+ const doc = parser . parseFromString ( html , 'text/html' ) ;
174
+
175
+ return doc . querySelector ( selector ) ;
176
+ }
177
+
161
178
/**
162
179
* @param {HTMLInputElement } field
163
180
* @return {boolean }
0 commit comments