Javascript 将用户的历史记录保存在一个网站上

Javascript 将用户的历史记录保存在一个网站上,javascript,jquery,ruby-on-rails,html,backbone.js,Javascript,Jquery,Ruby On Rails,Html,Backbone.js,我知道关于这个话题的帖子很少 然而,我想以稍微不同的方式提出这个话题: 我如何知道用户正在从特定页面返回,例如/product\u pages/3 我使用主干来跟踪用户历史,这是建立在RoR应用程序之上的。我想知道用户是否从特定页面返回的原因是为了让主干表单填充用户最初输入的值,但是如果用户来自其他页面,例如/provider\u pages/14,那么我想清除表单值 尝试过的解决方案: HTML5的历史在浏览器之间是不持久的(甚至在FF和Chrome之间),而且它也不是解决这个问题的最佳方案;

我知道关于这个话题的帖子很少

然而,我想以稍微不同的方式提出这个话题:

我如何知道用户正在从特定页面返回,例如
/product\u pages/3

我使用主干来跟踪用户历史,这是建立在RoR应用程序之上的。我想知道用户是否从特定页面返回的原因是为了让主干表单填充用户最初输入的值,但是如果用户来自其他页面,例如
/provider\u pages/14
,那么我想清除表单值

尝试过的解决方案:

HTML5的历史在浏览器之间是不持久的(甚至在FF和Chrome之间),而且它也不是解决这个问题的最佳方案; 主干历史并没有起作用,因为它只跟踪主干应用程序,每当用户退出主干应用程序时,Backbone.history就会丢失状态; RoR请求也不是一个解决方案,假设大多数用户将单击“后退”按钮返回表单/搜索结果,“后退”按钮不会触发新请求:)

所以我对这个问题的解决方案有点困惑

以下是代码位:

表格:

<ul id="formfields">
    <li>
        <label>Driver type
        <select id="driver-type" name="driver-type" class="ddl jqrequired">
            options...
        </select></label>
    </li>
    <li>
        <label>Postcode
        <input type="text" name="postcode" id="postcode_with_suburbs" style="width:207px" class="jqrequired postcode-with-suburb"></label>
    </li>
    <li id="date_insurance_required_section">
        <label>Date insurance required
        <input type="text" name="date_insurence_required" id="date_insurance_required" style="width:207px" class="jqrequired"></label>
        <span style="display:none;" id="renewalDate"></span>
    </li>
    <li id="car-insurers-section">
        <label>Your current car insurer (if applicable)
        <input type="text" id="car-insurers" style="width:220px" class="ddl clearfix" class="jqrequired" placeholder="Please enter current insurer">
        </label>
    </li>
    etc etc etc.....
一些暗示

  • 将服务器每次访问的最后路径->时间戳写入cookie

  • 将访问历史记录写入localStorage(大多数现代浏览器都支持此功能)


我喜欢本地存储的想法
$("#car-insurers").val(results.Insurer);
$("#renewal_date").val(results.RenewalDate);
$("#date_insurence_required").val(results.InsuranceRequired);
$("#renewal-type").val(results.RenewalType);
$("#renewal-amount").val(results.RenewalAmount);
$("#driver-type").select2("val", results.DriverType);
$("#postcode_with_suburbs").val(results.Postcode);
$("#email").val(results.EmailAddress);
if (results.EmailSend == "on") {
    $('#email-consent').prop('checked', true);
}