When doing a Web project encountered a requirement, when the page has no predecessor history (is currently the new pop-up page, can not do GoBack operation that is History.go (-1)), click the return button directly close the page, or back to the previous page.
The problem is how to determine whether there are history can be back, this is very troublesome, because there is no such function can be directly obtained, only through the history.length variable to do the processing, but for IE, and non ie length of the return value is different, ie: History.length=0, not IE is 1, so write a function to achieve the previous requirements of this function. Share to everyone.
/**
* Return to previous page (or close this page)
* <li> If there is no previous page history, direct close the current page </li>
*
/function GoBack () {
if ( Navigator.userAgent.indexOf (' msie ') >= 0) && (navigator.userAgent.indexOf (' Opera ') < 0)) {//IE
if ( History.length > 0) {
window.history.go (-1);
} else{
window.opener=null;window.close ();
}
else{//Non IE browser
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.opener=null;window.close ();
}
else{//Unknown browser
window.history.go (-1);
}}
}