JS to determine when the page can not be rolled back when the page is closed or history.go ( -1) _javascript tips

Source: Internet
Author: User

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); 
}} 
} 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.