HTML 5历史api只能从索引页工作
HTML 5 history api only work from index page
我有这个HTML索引页,问题是历史API(只加载想要的。jsp内的div和改变uri)只从索引页工作,如果我做一个刷新或在uri中输入,它只显示所需的。jsp,而不是菜单:
ps:我正在使用MVC (VRaptor)
index . jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<!-- font-awesome icons -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/font-awesome.min.css">
<!-- Bootstrap -->
<link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div style="width:100%;">
<div id="menu" style="float:left; width:20%;">
</div>
<div id="header" class="well">Bem vindo<b> ${userLogado.nome }</b></div>
<div id="conteudo" style="float:right; width:80%; ">
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/js/menuLoad.js"></script>
</body>
</html>
jquery:
$(document).ready(function(){
$("#menu").load("/restrict/menu");
});
$('a').click(function() {
pageurl = $(this).attr('href');
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
$('#conteudo').load($(this).attr('href'), function(responseText, statusText, xhr)
{
if(xhr.status == 403) {
$('#conteudo').load("/error/error403");
}
if(xhr.status == 404) {
$('#conteudo').load("/error/error404");
}
if(xhr.status == 405) {
$('#conteudo').load("/error/error405");
}
/*
if(statusText == "success")
alert("Successfully loaded the content!");
if(statusText == "error")
alert("An error occurred: " + xhr.status + " - " + xhr.statusText);
*/
});
return false;
});
所以每次调用这个JS然后加载# contteudo内的href,但我直接在uri/restrict/menu中输入它只显示未格式化的引导菜单,因为bootstrap. JS, bootstrap.css for bootstrap是在index.jsp而不是在menu.jsp中,还有另一种解决方案的情况下,当用户做刷新不只是显示所需的。jsp,但也index.jsp,但显示# contteudo内写的链接?
对不起,如果我不清楚
历史API允许用户通过点击"后退"或"下一步"按钮来操作历史堆栈的内容。Url将被替换为新的Url,但不会导致浏览器从新的Url加载文档。它只是将状态对象推入堆栈,然后更改URL。
似乎你正在使用PJAX技术来异步加载部分内容,而不是加载整个页面。输入uri或刷新页面将向服务器发送请求,浏览器将加载新页面。因此,您必须在后端识别请求的类型-如果是"PJAX"请求,则返回部分内容,如果是正常请求,则返回完整页面。
例如,我假设你的网页结构是这样的:
/index
------/about
------/experience
------/contact
当用户点击主页上的链接时,即使用户通过输入/index/about url访问页面,也要异步加载这三个链接的部分内容。
第1步:添加自定义头"X-PJAX",使后端可以识别请求的类型
pageurl = $(this).attr('href');
$.ajax({
url: $(this).attr('href'),
dataType: "html",
beforeSend: function(xhr){
xhr.setRequestHeader('X-PJAX', 'true')
},
success: function(response){
$('#conteudo').empty().append(response);
history.pushState({path:pageurl},'',pageurl)
}
});
步骤2:识别请求类型并在后端返回适当的响应
if(header contains X-PJAX header){
//return partial content
}else{
//return full page
}
希望你已经找到解决问题的办法了。但是希望有同样问题的人能从我的回答中得到帮助。
我有一个类似的问题,这是我如何解决它。我参考了http://diveintohtml5.info/history.html
你需要在你的函数中使用这行代码来改变URL,这行代码用来加载
内容 history.pushState(null,null,strURL);
然后使用它来处理浏览器导航按钮的点击
function navigate(href) {
var req = new XMLHttpRequest();
req.open("GET", "YOUR URL"+href.split("/").pop(),false);
req.send(null);
if (req.status == 200) {
document.getElementById("div1").innerHTML = req.responseText;
$("#div").load(" #div").hide().fadeIn(1000);
return true;
}
return false;
}
window.addEventListener("popstate", function(e) {
navigate(location.pathname);});
e.preventDefault();
}
- 下拉列表在使用z索引放置在前面后停止工作
- z索引堆栈系统未按预期工作
- Javascript表排序工作得很好,但对所有索引进行迭代会得到不同的结果
- 索引在IE中导致错误;在其他浏览器中工作
- 如果我对索引进行硬编码,它会按预期工作,为什么这个增量器会返回NAN
- 多设备混合应用程序 - 有没有人让 android 的索引数据库插件工作
- 不透明度和z索引的滑块无法正常工作
- 当在数组中给定动态索引时,ng重复不工作
- Z索引无法在位置固定的情况下工作
- jQuery切片负索引不工作
- 索引工作良好,即使图像是随机显示的
- Zurb Joyride:从索引#开始,不工作
- PhoneGap Javascript不能在索引文件中工作
- z索引不工作与jquery css
- 为什么我的Jquery Ajax请求不工作错误:未定义的索引:ProID
- Lodash查找索引不工作
- 为什么我的索引视图不工作时,我在我的rails应用程序实现日期picker
- 剑道表条(索引)不工作
- Jquery对滚动加载不工作(不更新索引)
- 字符串索引器在本地工作,但在网站托管时不起作用