Js determines that when the page cannot be rolled back, the page will be closed; otherwise, the page will be history. go (-1), jshistory. go

Source: Internet
Author: User

Js determines that when the page cannot be rolled back, the page will be closed; otherwise, the page will be history. go (-1), jshistory. go

When creating a Web project, you may encounter a requirement. When the page does not have a pre-history record (that is, the new pop-up page, you cannot perform the goback operation, that is, history. go (-1). Click the return button to close the page. Otherwise, the page is returned to the previous page.

The problem is how to determine whether there is history that can be rolled back. This is very troublesome, because no such function can be obtained directly, only through history. the length variable is flexible, but the return values for IE and non-IE lengths are different, ie: history. length = 0, and non-IE is 1. Therefore, a function is written to implement the previously required function. Share with you.

/*** Return to the 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 ('msi')> = 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 );}}}


JS rollback historygo (-1) does not return to the specified page

Logons must be processed in the background. After processing, you can directly jump to the target page without using js. If you log on Via ajax, you can send the target address to the backend, and then directly set location = url.
 
When the page calls historygo (-1) and returns to the previous page, how does one load the JS function of the previous page at the same time?

I think you can write the JS method of the previous page to onload ~~ So that it will automatically load JS every time it returns to this page

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.