在 R shiny App 中禁用浏览器后退按钮
Disable browsers back button in R shiny App
我正在构建一个闪亮的应用程序,它有很多条件面板。我在应用程序本身中有一个后退按钮,用于在条件面板之间导航。我想禁用网络浏览器后退按钮,因为单击该按钮会转到上一个网页(远离我的应用程序)。有办法吗?
你可以写一些 javascript
来做到这一点。这里我有两个例子,注意我只在 Chrome
上测试过
示例 1 将 return 在浏览器中激活 back
按钮时显示一条消息
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))
示例 2 将完全禁用导航。我个人不喜欢这种方法,因为人们可能会对您的网站不提供标准导航功能感到恼火
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))
我正在构建一个闪亮的应用程序,它有很多条件面板。我在应用程序本身中有一个后退按钮,用于在条件面板之间导航。我想禁用网络浏览器后退按钮,因为单击该按钮会转到上一个网页(远离我的应用程序)。有办法吗?
你可以写一些 javascript
来做到这一点。这里我有两个例子,注意我只在 Chrome
示例 1 将 return 在浏览器中激活 back
按钮时显示一条消息
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))
示例 2 将完全禁用导航。我个人不喜欢这种方法,因为人们可能会对您的网站不提供标准导航功能感到恼火
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))