简体   繁体   English

后退按钮重写以重定向回到新位置

[英]back button rewrite to redirect back to new location

I have a situation where I want to rewrite the back history for example I ma at my page 我有一种情况需要重写历史记录,例如我在页面上

http://mysite.com

and I go to another page by clicking a link 然后我通过单击链接转到另一个页面

http://mysite.com/sports

Now when I click back, I want to be able to have a parameter appended to the url so by click back the users will actually need to go here http://mysite.com?backfrom=sports Now I know that html5 has apis that I can use but for some reason its not for me on my http://mysite.com/sports javascript I add this 现在,当我单击返回时,我希望能够将一个参数附加到url上,因此,通过单击返回,用户实际上将需要转到此处http://mysite.com?backfrom=sports。现在,我知道html5具有api我可以在我的http://mysite.com/sports javascript中使用它,但由于某种原因它不适合我添加

<script>
 history.replaceState(null, null, "?backfrom=sports");
</script>

but that just change the url and when I click the back button, I still end up at http://mysite.com 但这只是更改了网址,当我单击“后退”按钮时,我仍然以http://mysite.com结尾

What can I do to change the history in a way that when I click back button it includes the parameters. 我该怎么做才能更改历史记录,方法是单击后退按钮时它包含参数。 I think there is onpopstate but I am not sure how can I make use of it. 我认为这里有流行状态,但我不确定如何使用它。

I think of a possible solution which could be to add the script in the index page of http://mysite.com like this 我认为可能的解决方案可能是像这样在http://mysite.com的索引页中添加脚本

<script>

$('body').on('click','.mylink', changehistory);

function changehistory(){

  history.pushState(null,null,'?backfrom=sports');

}

</script>

this will fake the url and now when I click back from 这将伪造URL,现在当我从

http://mysite.com/sports http://mysite.com/sports

I get back to 我回到

 http://mysite.com?backfrom=sports

Thought I would share thanks 以为我会分享谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM