javascript - 如何使 Bootstrap Tour 应用 GET 参数

标签 javascript twitter-bootstrap get bootstrap-tour

我有一个搜索系统,其主页位于:

http://example.com/ 

结果页面(完全不同)位于:

http://example.com/?q=foo

我正在尝试设置 Bootstrap 浏览,以便它从主页开始,然后将用户带到结果页面。所以我有这样的步骤:

        {
            path: '/',
            element: "#extra-sidebar-fields",
            backdrop: true,
            title: "Sophisticated Search",
            content: "In the Advanced Search area, you can make sophisticated searches against many fields. " +
                "Press \"Next\" and we'll make a query for you.",
        },
        {
            path: '/?q=roe+v+wade',
            element: 'article:first',
            title: 'Detailed Results',
            content: 'Here you can see the results for the query "Roe v Wade" sorted by relevance.'
        }

我遇到的问题是,游览不明白它需要执行 GET 请求来加载第二步,所以我想我需要使用 redirect 键或其他东西。

如果我在不同的路径上有搜索结果,这会非常有效,但由于主页和结果都位于 / ,看来我被卡住了。

编辑...

自从发布这篇文章以来,我在公开工作中尝试了一些事情。首先,我认为我可以通过使用 onNext 参数来完成重定向,因此我将第一步更改为:

     {
        path: '/',
        element: "#extra-sidebar-fields",
        backdrop: true,
        title: "Sophisticated Search",
        content: "In the Advanced Search area, you can make sophisticated searches against many fields. " +
            "Press \"Next\" and we'll make a query for you.",
        onNext: function(){
            window.location = '/?q=row+v+wade'
        },
    },
    {
        path: '/?q=roe+v+wade',
        element: 'article:first',
        title: 'Detailed Results',
        content: 'Here you can see the results for the query "Roe v Wade" sorted by relevance.'
    }

但这不起作用,因为虽然重定向正确发生(很好!),但在重定向之前,用户会看到弹出下一步,并且根据他们的连接速度,在重定向之前它可能会显示一段时间完成的。因此,除非我能够阻止下一步的出现,否则该策略是没有用的。

<小时/>

我尝试了另一种策略,重新定义 _isRedirect 方法:

tour._isRedirect = function(path, currentPath){
    return (path != null) && //Return false if path is undefined.
        path !== "" && (
        ({}.toString.call(path) === "[object RegExp]" && !path.test(currentPath)) ||
            ({}.toString.call(path) === "[object String]" &&
                path.replace(/\/?$/, "") !== currentPath.replace(/\/?$/, "")
                )
        );
}

通常它有一行删除 GET 参数 (path.replace(/\?.*$/, "")),所以我修改它不这样做。这个改变也起到了一定的作用。重定向正确发生,没有显示下一步,但不幸的是,这个更改使它有一个重定向循环......或类似的东西。它只是一遍又一遍地重定向。

<小时/>

不确定接下来要做什么。可以寻求更精通 JS 的人的帮助。

最佳答案

尝试将行号 431 更改为此

   return (path != null) && path !== "" && (({}.toString.call(path) === "[object RegExp]" && !path.test(currentPath)) || ({}.toString.call(path) === "[object String]" && path !== currentPath));

默认情况下,该行会去掉“?”对于 currentPath 和 path。因此,当 path = "/?q=roe+v+wade"currentPath = "/"q=roe+v+wade被剥离,因此路径和 currentPath 相等,因此不会启动重定向。

编辑:发现我最初的答案存在问题。将第 292 行更新为

    current_path = [document.location.pathname, document.location.search, document.location.hash].join("");

current_path 变量(输入 isRedirect 函数)最初仅获取路径名和哈希值。 document.location.search 的添加告诉它也获取 get vars - 但我必须说,我不能 100% 确定浏览器兼容性。

关于javascript - 如何使 Bootstrap Tour 应用 GET 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23959416/

相关文章:

javascript - 通过回调发送 Google Charts 选项

javascript - html anchor 标签onclick()和href同时执行

javascript - AngularJS ng-repeat 和 bootstrap 以显示来自数据库的列中的元素

php - 将 PHP 变量传递给 Bootstrap 模式

android - MVVMCross Native Android Progressbar 使用当前进度移动元素

web-services - 为什么我们在 SOAP 中没有 GET 调用?

javascript - 我可以将变量从 flash 发送到 javascript,但无法执行相反的操作

javascript - 如何使用 axios 发送对象?

javascript - JavaScript 碰撞函数

jquery - 如何让 Jeditable 使用 Bootstrap 按钮类来确定/取消?