turbolinks / turbolinks-classic

Classic version of Turbolinks. Now deprecated in favor of Turbolinks 5.
MIT License
3.54k stars 428 forks source link

Suggestions for workaround for Turbolinks 2.5 to run script on forward/back buttons #632

Closed justin808 closed 9 years ago

justin808 commented 9 years ago

The README.md for master clearly states how to handle the forward/back buttons with an inline script that needs to run when the page is loaded.

How should this be handled when using 2.5?

How about if the script is wrapped in a function used in a callback to the document like:

 function pageLoaded() { console.log("page loaded"); }
 function pageUnloaded() { console.log("page unloaded"); }

 document.addEventListener('page:before-unload', pageUnloaded);
 document.addEventListener('page:change', pageLoaded);

Is that a suitable alternative to not having the: data-turbolinks-eval=always feature in v3?

We're looking for the best solution for https://github.com/shakacode/react_on_rails/issues/105 which enables React components to work with TurboLinks.

Thibaut commented 9 years ago

I think this is correct. page:before-unload and page:change fire on all page changes, including back/forward. Same in master, except that those events are now receiving the affected DOM elements (in event.data).