Disable browsers back button in R shiny App

亡梦爱人 提交于 2019-12-31 03:48:11

问题


I am building a shiny app which has a lot of conditional panel. I have a back button in the app itself to navigate between the conditional panel. I would like to disable the web browsers back button as clicking that button goes to previous webpage(away from my app). Is there a way to do this?


回答1:


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))


来源:https://stackoverflow.com/questions/38435890/disable-browsers-back-button-in-r-shiny-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!