使用HTML5 History时如何处理页面刷新?
我已经阅读了大量的教程和示例但无济于事,这导致我在这里提出我的问题。我正在开发一个带有固定侧面导航和动态内容 div 的网页。
问题
当我点击链接“加载”时,内容 div 将显示我的其他页面“loadcourses.php”中的内容,然后使用历史 api 将 url 更改为“http:” //mydomain.com/main.php?page=loadcourses'。基本上 main.php 不会改变,我只会添加其他页面的标题并将其标记为链接后面的参数。
接下来,从动态加载的内容中,我有一些链接可以在单击时显示每个单独课程的内容。关键部分就在这里,一旦我点击任何链接,我就应该解析下一页的另一个参数,即“course.php”。我已经设置了 href='http://mydomain.com/course.php?cid=CSC101'。现在,我第一次使用动态 div 加载内容并成功检索下一页的 id CSC101 时没有问题。但是当我点击刷新时,我的 cid 丢失了。
根据历史 API,后退/前进按钮可以正常工作。这里需要帮助,谢谢!我目前正在研究这个 .jsp 原型。
document.addEventListener('DOMContentLoaded', init, false);
function hasBrowserHistory(){return !!(window.history && history.pushState);}
function updateHistory(url, replaceHistory) {
//Create a page path
var path = "?page=" + url.split(".")[0];
var object = {
data: url
};
if(replaceHistory){
//alert('as');
/*
If this is the first page we are loading
replace the current history item.
*/
history.replaceState(object, null, path);
}else{
//alert('qw');
/*
If we got here by clicking one of the links
add a new item to the browser history
*/
history.pushState(object, null, path);
}
}
function loadPage(whichPage, replaceHistory) {
$('#contentCol').load(whichPage);
updateHistory(whichPage, replaceHistory);
}
function historyPopStateHandler(e) {
//var state = history.state;
if (e.state == null) {
alert(e.state.url);
}
if(e.state != null) {
loadPage(e.state.data, true);
}
}
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
function init() {
//Check if the history API is available
if(!hasBrowserHistory()){
alert('Sorry dude, your browser does not support the history API');
return;
}
//Check if we have any url params (e.g bookmark or reload)
var params = getUrlVars();
if(params['page'] != undefined && params['page'] != null){
//Load the holder page with the correct content
loadPage(params['page'] + ".jsp", true);
}else{
//Load the default page
loadPage('loadcourses.jsp', true);
}
//Setup listeners for the links
jQuery('nav > a').click(function(e){
e.preventDefault();
loadPage(jQuery(e.target).attr('href'), false);
});
//Setup listeners for the history events
window.addEventListener('popstate', historyPopStateHandler);
}
I have read through tons of tutorials and examples to no avail which leads me to ask my question here. I am developing a webpage with a fixed side navigation with a dynamic content div.
Problem
When i click on a link let's say 'load', the content div will display the content from my other page 'loadcourses.php' then using the history api i will change the url to 'http://mydomain.com/main.php?page=loadcourses'. Basically the main.php will not change i will just add the title of the other page and tag it as a parameter behind the link.
Next, from the dynamically loaded content, i have some links to show the content of each individual course when clicked. The crucial part lies here, once i clicked on any link, i am supposed to parse another parameter to the next page which is 'course.php'. I have set my href='http://mydomain.com/course.php?cid=CSC101'. Now i have no problem loading the first time with the dynamic div loading the content and successfully retrieving the id CSC101 at the next page. But when i hit refresh, my cid is lost.
The back/forward button works fine as per the history api. Help needed here, Thanks!! I am currently working on this .jsp prototype.
document.addEventListener('DOMContentLoaded', init, false);
function hasBrowserHistory(){return !!(window.history && history.pushState);}
function updateHistory(url, replaceHistory) {
//Create a page path
var path = "?page=" + url.split(".")[0];
var object = {
data: url
};
if(replaceHistory){
//alert('as');
/*
If this is the first page we are loading
replace the current history item.
*/
history.replaceState(object, null, path);
}else{
//alert('qw');
/*
If we got here by clicking one of the links
add a new item to the browser history
*/
history.pushState(object, null, path);
}
}
function loadPage(whichPage, replaceHistory) {
$('#contentCol').load(whichPage);
updateHistory(whichPage, replaceHistory);
}
function historyPopStateHandler(e) {
//var state = history.state;
if (e.state == null) {
alert(e.state.url);
}
if(e.state != null) {
loadPage(e.state.data, true);
}
}
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
function init() {
//Check if the history API is available
if(!hasBrowserHistory()){
alert('Sorry dude, your browser does not support the history API');
return;
}
//Check if we have any url params (e.g bookmark or reload)
var params = getUrlVars();
if(params['page'] != undefined && params['page'] != null){
//Load the holder page with the correct content
loadPage(params['page'] + ".jsp", true);
}else{
//Load the default page
loadPage('loadcourses.jsp', true);
}
//Setup listeners for the links
jQuery('nav > a').click(function(e){
e.preventDefault();
loadPage(jQuery(e.target).attr('href'), false);
});
//Setup listeners for the history events
window.addEventListener('popstate', historyPopStateHandler);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我听到了同样的问题,但我修复了它......它是如此简单,当你点击刷新时,浏览器将从服务器请求页面,并且该页面不会在那里......
为了满足这一点,您需要一个 .htaccess 文件,该文件将用于将这些请求重定向到相应的 php 文件...就像如果 url 是 www.you.com/coding,.htaccess 文件将扫描单词编码,将请求重定向到coding.php...
在php中,您需要获取请求的url $_SERVER['REQUEST_URI'] 然后进行一些过滤...
如果请求满足过滤器...在该 php 上嵌入一个 html 页面
布拉..
?>
因此,在 html 中放入加载表单时会出现的值...这就是我什至在我的网站中尝试过的逻辑 deenze 成功了
yea guyz i heard the same problem but i fixed it... its so simple, when you hit refresh, the browser will request the page from the server and the page would not be there...
to cater for this you need a .htaccess file that will be used in redirecting those requests to the respective php file... like if the url is www.you.com/coding, the .htaccess file will scan for the word coding, the redirect the request to coding.php...
inside the php, you need to get the url of the request $_SERVER['REQUEST_URI'] then do some filtering...
if the request satisfy the filters... have a html page embeded on that php
bla..
?>
so in the html put the values that would have appered when you loaded the form... and thats the logic i even tried it in my website deenze and it worked