js返回上一页的实现

如果有上一页则返回上一页,如果没上一页(直接输入url打开的网页)则返回首页。兼容主流浏览器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 返回
*/
function goBack() {
if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)) {
// IE
if (history.length > 0) {
window.history.go(-1);
} else {
window.location.href = ctx + "/";
}
} else {
// 非IE浏览器
if (navigator.userAgent.indexOf('Firefox') >= 0 ||
navigator.userAgent.indexOf('Opera') >= 0 ||
navigator.userAgent.indexOf('Safari') >= 0 ||
navigator.userAgent.indexOf('Chrome') >= 0 ||
navigator.userAgent.indexOf('WebKit') >= 0) {

if (window.history.length > 1) {
window.history.go(-1);
} else {
window.location.href = ctx + "/";
}
} else {
// 未知的浏览器
window.history.go(-1);
}
}
}