Cancelling visits before they begin
suggest changeTurbolinks provides an event listener that can be used to stop visits from occurring. Listen to the turbolinks:before-visit event to be notified when a visit is about to commence.
In the event handler, read the visit URL from the event, then cancel it with preventDefault:
// plain JavaScript
document.addEventListener("turbolinks:before-visit", function(event) {
event.data.url
event.preventDefault()
})
// jQuery: the Turbolinks event is on originalEvent
$(document).on("turbolinks:before-visit", function(event) {
event.originalEvent.data.url
event.preventDefault()
})
NOTE:
According to the official turbolinks docs:
Restoration visits cannot be canceled and do not fire turbolinks:before-visit.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents