Javascript 让Backbutton在单页网站中工作并实现;“说话”;网址
我有一个单页网站,希望实现以下目标:Javascript 让Backbutton在单页网站中工作并实现;“说话”;网址,javascript,html,.htaccess,single-page-application,Javascript,Html,.htaccess,Single Page Application,我有一个单页网站,希望实现以下目标: 后退按钮的工作方式就像一个普通的网站 而不是说 www.mysite.com/index.php?p=#这是一款很棒的产品 我想要这个网址 www.mysite.com/this-is-a-great-product 同时仍然有后退按钮正常工作 关于1.)我使用了我发现非常有效的以下代码: <!-- Getting BackButton to work properly --> <script type="text/javascript"
<!-- Getting BackButton to work properly -->
<script type="text/javascript">
var times = 0;
function doclick() {
times++;
}
function doclick() {
times++;
location.hash = times;
}
window.onhashchange = function() {
if (location.hash.length > 0) {
times = parseInt(location.hash.replace('#',''),10);
} else {
times = 0;
}
}
</script>
这将/index.php?p=products更改为/products
那么,我如何更改上面的代码(在1下),使其不会将所有锚都更改为#1、#2等,而是引用/使用我在2下获得的URL,如
www.mysite.com/this-is-a-great-product
和(可能是一个非常愚蠢的问题,但也是一个非常重要的问题)-鉴于我只在我的网站上使用新的url链接-这是否仍有可能导致以任何方式重复内容
关于这一点,我是否应该(出于这个原因或任何其他原因)使用rel canonical link=index.php将我的单页index.php推荐给自己
非常感谢 HTML5提供了一个支持浏览器历史记录的api,您可以在不重新加载的情况下管理url 检查此链接 演示是
如前所述,您需要使用HTML5历史API。请注意,此API相对较新,因此需要考虑浏览器支持。在撰写本文时,大约71%的全球互联网用户支持它(有关浏览器支持信息,请参阅)。因此,您需要确保您有一个解决此问题的后备解决方案。您可能会希望使用较旧的#!在采用HTML5历史API之前流行的解决方案 例如,如果使用历史API替换,
example.com/#!设置
使用example.com/settings和用户为更好的URL添加书签,然后当他们访问该URL时,他们的浏览器将向服务器请求设置(在web服务器的上下文中实际上不存在设置)。因此,您需要确保您的web服务器具有一些重定向规则(即RewriteEngine),以便它可以获取漂亮的URL并将它们重定向到#!版本(如果用户的浏览器支持历史API,它可以用漂亮的URL替换)
若您自己编程不太舒服,我建议您使用一个JavaScript库,它可以为您做很多工作。我做了一些快速搜索,发现了以下内容,尽管可能还有更好的:基本上,我在
jsfiddle
上创建了一个小型原型,它跟踪通过ajax调用访问的所有URL
还包含来回访问链接的导航
它的实际工作原理:
$(function () {
var history = [];
var index = 0;
$('.links').on('click', function () {
$('#history').append($(this).text());
var address = $(this).attr('data-ref');
index += 1;
history[index] = address;
$('.links').attr('disabled', 'disabled');
loadExternalPage(address);
console.log('list:' + history);
});
$('#back').on('click', function () {
console.log(index);
index -= 1;
console.log(index);
console.log(history[index]);
loadExternalPage(history[index]);
});
$('#forward').on('click', function () {
console.log(index);
index += 1;
console.log(index);
console.log(history[index]);
loadExternalPage(history[index]);
});
var loadExternalPage = function (address) {
console.log(history[index]);
$('#result-section').load(address, function () {
console.log('data-loaded');
$('.links').removeAttr('disabled');
});
};
});
- 我创建了一个名为
的全局数组,它按顺序跟踪通过ajax访问的所有URL李>history
- 此外,还定义了一个全局
,用于在索引
数组中来回导航链接时跟踪所访问的urlhistory
- 在
的底部有jsfiddle
,它通过捕获链接名并按访问顺序发布链接来显示访问链接的顺序History部分
$(function () {
var history = [];
var index = 0;
$('.links').on('click', function () {
$('#history').append($(this).text());
var address = $(this).attr('data-ref');
index += 1;
history[index] = address;
$('.links').attr('disabled', 'disabled');
loadExternalPage(address);
console.log('list:' + history);
});
$('#back').on('click', function () {
console.log(index);
index -= 1;
console.log(index);
console.log(history[index]);
loadExternalPage(history[index]);
});
$('#forward').on('click', function () {
console.log(index);
index += 1;
console.log(index);
console.log(history[index]);
loadExternalPage(history[index]);
});
var loadExternalPage = function (address) {
console.log(history[index]);
$('#result-section').load(address, function () {
console.log('data-loaded');
$('.links').removeAttr('disabled');
});
};
});
现场演示@JSFiddle:
$(function () {
var history = [];
var index = 0;
$('.links').on('click', function () {
$('#history').append($(this).text());
var address = $(this).attr('data-ref');
index += 1;
history[index] = address;
$('.links').attr('disabled', 'disabled');
loadExternalPage(address);
console.log('list:' + history);
});
$('#back').on('click', function () {
console.log(index);
index -= 1;
console.log(index);
console.log(history[index]);
loadExternalPage(history[index]);
});
$('#forward').on('click', function () {
console.log(index);
index += 1;
console.log(index);
console.log(history[index]);
loadExternalPage(history[index]);
});
var loadExternalPage = function (address) {
console.log(history[index]);
$('#result-section').load(address, function () {
console.log('data-loaded');
$('.links').removeAttr('disabled');
});
};
});
注意:这个解决方案远不是完美的,所以不要认为它是最终的解决方案,而是把它作为建立在之上的基础。
- state对象—一个JSON,它包含足够的数据来重新创建存储的页面
- 页面标题我希望你也可以使用一个虚拟URL,但我会把它留给学生作为练习,就像他们说的那样