Disable browsers back button in R shiny App

戏子无情 提交于 2019-12-02 07:02:58

You can write some javascript to do this. Here I have two examples, note that I only tested this on Chrome

Example 1 will return a message upon activation of the back button within the browser

rm(list = ls())
library(shiny)
jscode <- 'window.onbeforeunload = function() { return "Please use the button on the webpage"; };'
ui <- basicPage(
  mainPanel(tags$head(tags$script(jscode)))
)

server <- function(input, output,session) {}
runApp(list(ui = ui, server = server))

Example 2 will disable navigation altogether. Personally I don't like this method as people might be annoyed that your site doesn't offer standard navigation functionalities

rm(list = ls())
library(shiny)
jscode2 <- "history.pushState(null, null, document.title);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.title);});"
ui <- basicPage(
  mainPanel(tags$head(tags$script(jscode2)))
)

server <- function(input, output,session) {}
runApp(list(ui = ui, server = server))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!