Bootstrap Tour 不记得我离开的地方

2023-11-27

当我到达第二页时,我在多页游览中启动 Bootstrap Tour 时遇到问题。

我以点击事件开始游览localStorage被设定为false。游览从单击事件开始正常,但是当我转到游览的第二步并加载新页面时,游览不会从中断处继续。

如何在这个新页面上从第二步恢复游览?我知道我需要重新初始化游览,但显然我没有正确执行此操作。

$(document).ready(function () {
    // Instance the tour
    var tour = new Tour({
        name: "CiteTour",
        steps: [{
            element: "",
            title: "#1",
            content: "You can find help with formatting citations on the Guide page. Click 'Next' to go there now.",
            placement: ""
        }, {
            element: "#CiteTour2",
            title: "#2 - Citation Resources",
            content: "There are several options for getting help with formatting citations. Once on the Guide page, look for the box labeled 'Citation Help.'",
            placement: "right",
            path: "/newpath",
            onNext: function (tour) {
                tour.init();
                tour.restart();
            }
        }, {
            element: "#CiteTour3",
            title: "#3",
            content: "This site can help format your research paper and references in APA, MLA, and the other major citation formats.",
            placement: "right",
        }, {
            element: "#AskTour1",
            title: "#4 - Ask-a-Librarian",
            content: "If you still have questions about citations or citation format, feel free to contact the librarians.  Good luck!",
            placement: "left",
        }],
        storage: false,
    });

    // Initialize the tour
    tour.init();
    $('#CiteTour-go').on('click', function () {
        // Start the tour
        tour.start();
    });
});

问题及解释

First, you must确保您正在使用storage: window.localStorage,它使用存储API。这是默认的游览选项,因此您所要做的就是not正如您所做的那样,将其覆盖为 false。这样做的目的是允许 Bootstrap Tour 在同一域内的多个页面上保留当前步骤信息。

Want Proof? - Open up your dev tools and see: Resources

Second,如果您指定path任何步骤的选项,您应该指定它所有步骤。当单页游览开始时,它不需要担心导航到不同的页面,但是一旦移动到新页面,如果您没有为前面的步骤指定路径,引导游览就无法进行知道要导航回哪里。

Furthermore, you need to use an absolute-path reference by prefxing the url with a single slash so it is relative to the root directory. If you use relative paths, the path will be changed as you move through pages/steps. For more info, see my section at the bottom on the Infinite Page Refreshing Issue

Third,只要您定义tour对象和init拨号后,游览将自动在新页面上进行。

让我们看一个简化版本what init() does:

Tour.prototype.init = function(force) {
  // other code omitted for brevity

  if (this._current !== null) {
    this.showStep(this._current);
  }
};

因此,一旦您初始化了游览,只要它注意到游览已经开始并且尚未结束(即它有当前步骤),它就会自动启动该步骤。所以你不需要通过点击来初始化onNext事件在你的第二步。

多页游览概念验证

可编辑的普朗克 | 可运行的演示

脚本.js

$(function() {
  
  // define tour
  var tour = new Tour({
    steps: [{
      path: "/index.html",
      element: "#my-element",
      title: "Title of my step",
      content: "Content of my step"
    }, {
      path: "/newPage.html",
      element: "#my-other-element",
      title: "Title of my step",
      content: "Content of my step"
    }]
  });

  // init tour
  tour.init();

  // start tour
  $('#start-tour').click(function() {
    tour.restart();
  });
  
});

索引.html:

<!DOCTYPE html>
<html>
<head>
  <title>Multipage Bootstrap Tour - Page 1</title>
  <link rel="stylesheet" href="bootstrap.css" />
  <link rel="stylesheet" href="bootstrap-tour.min.css">
</head>
<body>
  <div class="container">
    <h1>First Page</h1>
    
    <button class="btn btn-lg btn-primary" id="start-tour">
      Start Tour
    </button><br/><br/>
  
    
    <span id="my-element">
      My First Element
    </span>
    
  </div>
  <script src="jquery.min.js"></script>
  <script src="bootstrap.js"></script>
  <script src="bootstrap-tour.min.js"></script>
  <script src="script.js"></script>
</body>
</html>

新页面.html

<!DOCTYPE html>
<html>
<head>
  <title>Multipage Bootstrap Tour - Page 2</title>
  <link rel="stylesheet" href="bootstrap.css" />
  <link rel="stylesheet" href="bootstrap-tour.min.css">
</head>
<body>
  <div class="container">
    <h1>New Page</h1>
    
    <span id="my-other-element">
      My Second Elemennt
    </span>

  </div>
  <script src="jquery.min.js"></script>
  <script src="bootstrap.js"></script>
  <script src="bootstrap-tour.min.js"></script>
  <script src="script.js"></script>
</body>
</html>

您在哪里引入了以下内容图书馆:

  • 引导程序.css
  • bootstrap-tour.min.css
  • jquery.min.js
  • bootstrap.js
  • bootstrap-tour.min.js

无限页面刷新问题

在许多配置中,您将进入页面无限刷新的循环,不断尝试解析当前步骤的路径。以下是出现此问题的原因以及解决方法。

Bootstrap Tour 下一步如何进行?

When you hit the Next Button, the tour will call showStep(i) for the next step

这是一个简化版本showStep:

Tour.prototype.showStep = function (i) {
    // other code omitted for brevity

    // get step path
    path = tour._options.basePath + step.path;

    // get current path - join location and hash
    current_path = [document.location.pathname, document.location.hash].join('');

    // determine if we need to redirect and do so
    if (_this._isRedirect(path, current_path)) {
        _this._redirect(step, path);
        return;
    }
};

因此,如果文档中的当前路径与下一步的路径不同,则游览将自动重定向到下一步。

Here's a simplified form of the redirection that just takes into account string values:
I've omitted regex based paths although Bootstrap Tour also supports them

Tour.prototype._isRedirect = function(path, currentPath) {
    var checkPath = path.replace(/\?.*$/, '').replace(/\/?$/, '');
    var checkCurrent = currentPath.replace(/\/?$/, '');
    return (checkPath !== checkCurrent);
};

Tour.prototype._redirect = function(step, path) {
    this._debug("Redirect to " + path);
    return document.location.href = path;
};

Note:正则表达式只是用来删除查询参数(/\?.*$/) 和尾随正斜杠 (//?$/`)

当任何页面加载时,不确定 Bootstrap Tour 是否已重定向它,或者您只是回来并尝试从上次中断的地方继续浏览。

因此,在初始化游览时的任何页面上:

  1. 它将根据本地存储中的值启动当前步骤。
  2. 当当前步骤加载时,它将确认该步骤的路径与当前 url 相同
  3. 如果没有,它将重定向到步骤路径并从步骤 1 重新开始

换句话说,它知道如何到达下一步需要去的地方,但一旦到达那里就无法确认情况是否如此。以这种情况为例,步骤如下:

var step = {
      path: "index.html",
      element: "#my-element",
      title: "Title of my step",
      content: "Content of my step"
    }

它可以很好地重定向到相对引用,但是当页面再次加载并检查它是否已加载到正确的地址时,会发生这种情况:

infinite loop

“KyleMit”,你可能会抗议,“难道它不能弄清楚我想要什么吗?"

-No!

如果您依赖相对路径进行重定向,则当它加载步骤时,它无法保证您实际上已到达该步骤,并且它会尝试再次重定向您。

这是因为,在网址中,"index.html" !== "\index.html"。这是两条不同的路!一个保证位于域根,而另一个可以位于任何地方。想象一下您有一些像这样的嵌套视图:

  • Views
    • Shared
      • 索引.html
      • 新页面.html
    • Person
      • 索引.html
      • 新页面.html

在页面之间导航时,如果您只告诉引导程序正确的页面名称,引导程序如何知道它是否到达了正确的目的地。

这给我们带来了这个问题的解决方案:

使用绝对 URL 绝对

Tip: Get a better sense of what's going on by passing in debug:true when creating your tour, which will log every redirect:
redirect

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Bootstrap Tour 不记得我离开的地方 的相关文章

随机推荐

  • 从 Angular 中的 Promise 返回值

    我对 Promise 有点困惑 我在 Ionic Angular 中有以下提供程序 Injectable export class StorageProvider constructor public storage Storage sav
  • SQL Server exec 关键字

    T SQL中exec关键字的作用是什么 I tried EXEC sp rename mytable foo bar column and sp rename mytable foo bar column 两个命令产生的结果看似相同 If
  • Android - 通过 DDMS 分析特定线程(UI 线程)

    我试图找到在某些条件下我的应用程序中 UI 线程冻结的原因 我已通过 DDMS 分析运行该应用程序 但在底部树表视图中 我看到所有线程上调用的所有方法 而我只想关注主线程 有没有办法过滤 DDMS 中方法的底部树表列表 以仅包含所选线程 在
  • 在詹金斯工作中通过 FTP 下载文件?

    似乎只能使用以下命令通过 FTP 上传文件 https wiki jenkins ci org display JENKINS Publish Over FTP Plugin 但是我需要通过 FTP 下载一个文件 我可以为此编写一个 gro
  • 数组之前的 Ruby * 运算符[重复]

    这个问题在这里已经有答案了 可能的重复 理解范围和数组中的 ruby splat 谁能告诉我下面代码中 的作用是什么 line name yabbi language ruby Hash line split Thanks 是 splat
  • jquery validate需要点击submit两次才能提交表单

    我在模态中有一个表单 用户必须填写表单 否则验证将显示必填字段错误消息 然后单击提交输入类型 它什么也不做 然后再次单击它 然后它将通过ajax发布 并返回 谢谢 消息 我已经浏览了 Stackoverflow 并应用了我遇到的问题的答案
  • CPAN 首次启动(代理配置)

    我需要通过代理运行 cpan 但是当我尝试配置时出现错误 并且第一次无法进入 cpan root srv linux01 cpan CPAN pm requires configuration but most of it can be d
  • 使用 ASP.NET 日历控件时如何隐藏周末?

    有时 在显示日历时 需要防止显示周末日期和日期标题中的周末名称 有没有办法使用ASP NET 日历控件 由于提供了控件 因此在不重写控件的情况下无法执行此操作 执行此操作的一种方法是覆盖当天渲染 and Render在将输出发送回客户端之前
  • 在应用程序购买中,用户在应用程序处于后台时取消交易:交易状态保持在购买状态

    当应用程序处于后台且用户尚未登录商店时 我在应用程序购买 沙盒环境 中取消用户时出现一些奇怪的行为 流程如下 用户未登录 未在 设置 gt 商店 中设置应用程序 ID 用户点击购买按钮 这调用 SKPaymentQueue defaultQ
  • 动态地将可调用对象添加到类作为实例“方法”

    我实现了一个元类 它拆除用它创建的类的类属性 并根据这些参数的数据构建方法 然后将这些动态创建的方法直接附加到类对象 所讨论的类允许轻松定义 Web 表单对象在网络测试框架中使用 它一直工作得很好 但现在我需要添加一种更复杂类型的方法 为了
  • 在ios中使用查询字符串创建NSURLRequest

    我在 ios 中创建了一个用户查询表单 我使用 php 作为服务器端 我在 IOS 中构造了如下所示的查询字符串 http www mydomain in androidmail enquiry php name Vinoth Kumar
  • 如何将包含单引号的字符串保存到 PostgreSQL 中的文本列

    我在 Smartwcm Web 应用程序 SDK 中使用 hibernate 和 PostgreSQL 作为数据库 我在其中一个表中有一个字段 其数据类型是文本 当我尝试使用 hibernate 使用相应的 POJO 将值插入表中时 如果我
  • 无法使用 VS 2013 RTM 编译 OpenCV 2.4.5

    有人用 VS 2013 RTM 编译过 openCV 吗 我尝试在 IlmImf 模块中获取一堆 min 不属于命名空间 std max 不属于命名空间 std 并且 opencv features2d 无法编译并出现以下错误 opencv
  • 如何设置
    元素的箭头样式?

    我正在使用这段代码 另请参阅JSFiddle 以更改悬停时箭头的背景颜色 但是 这不起作用 因为箭头仅在单击时更改其颜色 summary webkit details marker color B6B6B6 font size 20px m
  • ClassInitialize 中生成的数据驱动测试:不再在 Visual Studio 2012 中工作

    我已从 Visual Studio 2010 升级到 Visual Studio 2012 在我的单元测试项目中 我有一个 ClassInitialize 方法 它生成一个 CSV 文件 然后使用连接到 CSV 的 DataSource 将
  • 数据库如何自动生成唯一标识符?

    我有一个带有 id guid 列的 sql 表 如何强制数据库为每条新记录自动生成新的 guid Add DEFAULT newid
  • Global.asax 中的 ASP.NET 路由

    我试图通过以下方式在我的 Web 表单应用程序中添加一条路线 http msdn microsoft com en us library cc668201 aspx adding routes to a web forms applicat
  • faces-redirect 和后退按钮导致其他链接无法正常工作

    我有一个关于面孔导航的问题 所以我有一个页面 它采用请求参数来加载特定用户 此页面显示 commandLink 列表 单击该列表后 将使用隐式导航重定向到另一个页面 通过调用 preRenderView 中的方法来加载用户 我们重定向到的页
  • 如何将协作者添加到 Firebase 应用程序?

    在最新版本的Firebase 在 Google I O 2016 期间宣布 如何将我想要的其他人添加到我的项目或应用程序中collaborate和 我通过以下途径认识了 IAM 角色Settings gt Permissions 这是正确的
  • Bootstrap Tour 不记得我离开的地方

    当我到达第二页时 我在多页游览中启动 Bootstrap Tour 时遇到问题 我以点击事件开始游览localStorage被设定为false 游览从单击事件开始正常 但是当我转到游览的第二步并加载新页面时 游览不会从中断处继续 如何在这个