最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to get go back two pages in javascript? - Stack Overflow

programmeradmin2浏览0评论

In javascript, I have a page A, which has a link to page B. B has javascript that redirects to another page C via window.location.href="". The problem is in IE 10/11, if i click back on page C, it goes back to page B, which redirects back to page C again. I want it so that it goes back to page A.

Can this be done?

Thanks

In javascript, I have a page A, which has a link to page B. B has javascript that redirects to another page C via window.location.href="". The problem is in IE 10/11, if i click back on page C, it goes back to page B, which redirects back to page C again. I want it so that it goes back to page A.

Can this be done?

Thanks

Share Improve this question asked Mar 21, 2017 at 20:18 omegaomega 44k90 gold badges286 silver badges523 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You can add your own back button, which will go back two pages instead of one, and set its onclick attribute to be:

window.history.go(-2)

That will bypass the redirect page.

EDIT: As @NelsonTeixeira clarified, you can create your own button, and label it whatever you'd like. For example:

<button onclick="window.history.go(-2)">Back</button>

Instead of blindly sending visitor of B to C you should use document.referrer to check where the user is ing from .

If referrer is A then goto C else goto A or if referrer is C goto A

Document.referrer at MDN

For example, you will put this code in page B:

var ref = document.referrer;

if(ref.includes("pageC.html")){
    window.location.href="pageA.html";
}
else{
    window.location.href="pageC.html";
}
发布评论

评论列表(0)

  1. 暂无评论