返回前一页,如果没有前一页历史,则跳转至 URL

发表于 2017-01-17 19:00:28   |   下载附件   |   字体:
返回上一页后退到上一页跳转至上一页或指定页面history.back
function GoBack(url) {
    if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)) { // IE  
        if (history.length > 0) {
            window.history.go(-1);
        } else {
            window.location.href = url;
        }
    } 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 > 2) {
                window.history.go(-1);
            } else {
                window.location.href = url;
            }
        } else {//未知的浏览器  
            if (history.length > 1) {
                window.history.go(-1);
            } else {
                window.location.href = url;
            }
        }
    }
}