我遇到了一个关于 pushState/popstate 的奇怪问题,我的初始页面似乎被保存了两次。
如果没有例子,有点难以解释。
像这样:
如您所见,我需要按两次后退按钮才能返回 www.bing.com,而它应该只需要按 1 次。
我的页面的简短说明(代码在页面底部):
当页面加载时,下拉选择的初始列表通过 AJAX 检索并填充选择标记。
用户可以选择点击 3 个链接来确定下拉选择的内容。示例:如果我点击选项 B 链接,这会触发与步骤 1 相同的 AJAX,但只会检索适合选项 B 的数据。
用户从下拉列表中进行选择并按下搜索。这会触发另一个 AJAX 函数,该函数检索相关数据并在 mysearchresults div 中显示格式化信息。
我尝试调整 pushState 的放置位置(例如,在 AJAX 调用之前或之后),现在看来这个位置给我带来的问题最少。虽然我确实遇到了我上面提到的问题。
这是我的代码。我不得不删除大量代码/函数以使其简短易读,但请确保 AJAX 调用/数据显示正常工作。
<div id="content">
<p>
<a class="coursetype" href="#" data-type="B">Option B</a>
| <a class="coursetype" href="#" data-type="H">Option H</a>
| <a class="coursetype" href="#" data-type="BG">Option BG</a>
</p>
<form id="myform">
<label class="formlabel" for="course">Select your course</label>
<br /><select id="course" name="course"></select>
<button id="searchbutton" type="button">Search Me</button>
</form>
<div id="mysearchresults"></div>
</div>
$(document).ready(function() {
var onClickCourseTypeHandler = function(event) {
event.preventDefault();
getCourses({ type:$(event.target).data("type") });
window.history.pushState({ html:$("#content").html(), coursecode:$("#coursecode").val() }, "", main?type="+pagevars.type);
}
var onClicksearchbuttonHandler = function(event) {
if ($("#coursecode").val().length > 0) {
searchDB({ course:$("#course").val() });
window.history.pushState({ html:$("#content").html(), coursecode:$("#coursecode").val() }, "", "/main?&course="+$("#course").val());
}
};
$(document).on("click", ".coursetype", onClickCourseTypeHandler);
$(document).on("click", "#searchbutton", onClicksearchbuttonHandler);
try {
window.addEventListener("popstate", function(event) {
if (event.state) {
$("#content").html(event.state.html);
if (event.state.coursecode.length > 0) {
if ($("#course option[value='" + event.state.course + "']").length > 0) {
$("#course").val(event.state.course);
} else {
$("#course option:first-child").attr("selected", "selected");
}
}
}
});
} catch(exception) {
}
onLoad();
function onLoad() {
getCourses({ type:"" });
window.history.pushState({ html:$("#content").html(), course:dbvars.course }, "", "/main");
}
function searchDB(parameters) {
$.ajax({
url: baseurl+"/searchDB"
, cache: false
, dataType: "json"
, data: { format:"json", course:parameters.course }
, success: function(data, status) {
parameters.data = data;
displaySearchResults(parameters);
}
});
}
function getCourses(parameters) {
if (typeof(parameters.cacheflag) === "undefined") { parameters.cacheflag = false; };
$.ajax({
url: baseurl+"/getCourses"
, cache: parameters.cacheflag
, dataType: "json"
, data: { format:"json", coursetype:parameters.type }
, success: function(data, status) {
parameters.data = data;
populateCourses(parameters);
}
});
}
});
最佳答案
当您加载页面时,它会被添加到历史记录中,因此当您执行 history.pushState 时,它会被第二次添加。
尝试在 onload 函数中将 history.pushState 替换为 history.replaceState。
关于javascript - pushState/popstate 的奇怪问题 - 初始页面似乎是 "pushed"两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29850174/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到rubygems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub