简体   繁体   English

如何关闭浏览器后退按钮上的彩色框弹出窗口

[英]How to close color box popup on browser back button

I want to close my color box popup on browser's back button click. 我想在浏览器的后退按钮上单击关闭我的彩色框弹出窗口。

I used an iframe and inside the iframe there are many links. 我使用iframe并在iframe内部有很多链接。 When user click on particular link then a color box popup will open. 当用户单击特定链接时,将打开一个颜色框弹出窗口。

Currently after opened the popup if user click on back button then popup is not closing. 目前,如果用户点击后退按钮打开弹出窗口,则弹出窗口没有关闭。

So i want such a functionality that if user click on back button then popup will be close and also page should not go to back (Disable back button). 所以我想要这样一个功能,如果用户点击后退按钮然后弹出窗口将关闭,页面也不应该返回(禁用后退按钮)。

I have used the following code but it's not working for me. 我使用了以下代码,但它对我不起作用。

<script>
    $(document).ready(function() {
        function disableBack() { window.history.forward() }

        window.onload = disableBack();
        window.onpageshow = function(evt) { if (evt.persisted) disableBack() }
    });
</script>

Please give me any suggestion to close the color box popup on back button or disable the back button completely. 请给我任何建议关闭后退按钮上的彩色框弹出窗口或完全禁用后退按钮。

You should use window.location.hash , and window.onhashchange . 您应该使用window.location.hashwindow.onhashchange Basically, after popup open, you change hash to something (like window.location.hash = "popup_opend" ). 基本上,在弹出窗口打开后,您将哈希值更改为某些内容(例如window.location.hash = "popup_opend" )。

Then when user clicks back, that back click will remove the hash you just added. 然后,当用户单击后退时,该后退单击将删除刚刚添加的哈希。 You just need to handle window.onhashchange to check for that and close the popup. 你只需要处理window.onhashchange来检查它并关闭弹出窗口。

Of course, hope you don't have any other code that manipulate the hash. 当然,希望你没有任何其他操纵哈希的代码。

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

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