When making a Web project encounter a requirement, when the page has no precursor history (that is, the current is a new pop-up page, can not do GoBack operation is history.go (-1)), click the Back button to close the page directly, or return to the previous page.
The problem is how to determine whether there is a history can be rolled back, this is very troublesome, because no such function directly can be obtained, only through the history.length variable to do the work, but for IE, and non-ie length of the return value of different, ie: History.length=0, non-IE is 1, so write a function to implement the function required earlier. Share to everyone.
/** * Return to previous page (or close this page) * <li> If there is no previous page history, close the current page directly </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); } }}