Cancelling visits before they begin

suggest change

Turbolinks 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents