关闭返回按钮上的弹出框
Close pop up on back button
我想关闭弹出点击后退按钮的移动。我使用onhashchange:
实现了这一点window.onhashchange = function (event) {
};
在这种情况下,如果弹出窗口被打开多次,然后点击后退按钮,它打开和关闭模态弹出窗口。但是,我希望模态弹出窗口在第一次返回时关闭,并在下一次返回时导航到前一页。
我也尝试使用onbeforeunload,但它会显示另一个警告离开或留在页面上。
$(window).bind('beforeunload', function(e) {
return false;
});
关闭返回按钮上的弹出窗口并重定向到下一个返回页面的最佳方法是什么?
if (window.history && window.history.pushState) {
$('#myModal').on('show.bs.modal', function (e) {
window.history.pushState('forward', null, './#modal');
});
$(window).on('popstate', function () {
$('#myModal').modal('hide');
});
}
bootply.com在我测试我的答案时关闭了。请参阅下面代码底部的内联脚本和注释。剩下的只是Twitter Bootstrap的样板,这样我就可以轻松地在本地进行测试。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>modal.html</title>
<!-- Bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<p>If you press the back button now, you should return to whatever page you were on before this page.</p>
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Show me the modal!</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<p>If you press the web browser's back button OR the modal's close buttons, the modal will close and the hash will return to "modal.html#modalClosed".</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script type="text/javascript">
// Immutable hash state identifiers.
var closedModalHashStateId = "#modalClosed";
var openModalHashStateId = "#modalOpen";
/* Updating the hash state creates a new entry
* in the web browser's history. The latest entry in the web browser's
* history is "modal.html#modalClosed". */
window.location.hash = closedModalHashStateId;
/* The latest entry in the web browser's history is now "modal.html#modalOpen".
* The entry before this is "modal.html#modalClosed". */
$('#myModal').on('show.bs.modal', function(e) {
window.location.hash = openModalHashStateId;
});
/* When the user closes the modal using the Twitter Bootstrap UI,
* we just return to the previous entry in the web
* browser's history, which is "modal.html#modalClosed". This is the same thing
* that happens when the user clicks the web browser's back button. */
$('#myModal').on('hide.bs.modal', function(e) {
window.history.back();
});
</script>
</body>
</html>
这是我对引导模态的解决方案。它为所有引导模态添加了后退按钮关闭的支持。你可以把它应用到你的非引导式弹出框中。
//Modal Closer With Back Button Support (Uses EventDelegation, so it works for ajax loaded content too.)
(function() {
var activeOpenedModalId = null;
var isHidingModalByPopState = false;
$(document).on('show.bs.modal', '.modal', function() {
activeOpenedModalId = $(this).attr('id');
window.location.hash = activeOpenedModalId;
}).on('hide.bs.modal', '.modal', function() {
if(!isHidingModalByPopState) {
window.history.back();
}
isHidingModalByPopState = false;
activeOpenedModalId = null;
});
$(window).on('popstate', function() {
if(activeOpenedModalId && window.location.hash !== '#'+activeOpenedModalId) {
isHidingModalByPopState = true;
$("#" + activeOpenedModalId).modal('hide');
}
});
})();
我为自己的网站写了这段代码
在不同的设备和浏览器上测试了太多次
铬71 , 铬67 , 火狐65 , Android 4.1 , Android 4.2 , Android 7.1
window.onload=function(){
State=0;
$(".modal").on("show.bs.modal",function(){
path=window.location.pathname+window.location.search;
history.pushState("hide",null,path);
history.pushState("show",null,path);
State="show";
})
.on("hidden.bs.modal",function(){
if(!!State)
history.go(State=="hide"?-1:-2);
});
setTimeout(function(){// fix old webkit bug
window.onpopstate=function(e){
State=e.state;
if(e.state=="hide"){
$(".modal").modal("hide");
}
};
},999);
$("#myModal").modal("show");
};
- 不要用
$(document).ready
代替window.onload
这可以很容易地完成使用Apache Cordova但不确定如果你使用它来显示你的网页在webview
function onBackKeyDown(e) {
e.preventDefault();
}
document.addEventListener("backbutton", onBackKeyDown, false);
http://cordova.apache.org/docs/en/2.4.0/cordova_events_events.md.html backbutton
根据http://www.mylearning.in/2015/06/close-modal-pop-up-on-back-button.html
$('#myModal').on('show.bs.modal', function(e) {
window.location.hash = "modal";
});
$(window).on('hashchange', function (event) {
if(window.location.hash != "#modal") {
$('#myModal').modal('hide');
}
});
通过点击返回,自动激活$('.modal').hide()函数。所以不需要隐藏modal。我们可以看到后退按钮后的灰色阴影背景。您可以使用以下任意一行代码关闭模式弹出窗口。
- $('。模态")。Modal ('hide')。数据(bs。Modal ', null);
或
-
$(身体).removeClass (modal-open);
$ (' .modal-backdrop ') .remove ();
检查页面时,modal是活跃的,你可以看到这些类。如果这个方法是错误的,或者存在其他简单的方法,请纠正我。
将下面的代码添加到脚本中:
//update url to current url + #modal-open. example: https://your-url.test/home#modal-open
$('body').on('shown.bs.modal', '.modal', function () {
location.hash = '#modal-open'
})
//remove #modal-open from current url, so the url back to: https://yoururl.test/home
$('body').on('hidden.bs.modal', '.modal', function () {
location.hash = ''
})
//when user press back button
$(window).on('hashchange', function (e) {
if(location.hash == ''){
$('.modal').modal('hide')
}
})
和上面的代码可以完美地工作,即使模态打开和关闭了几次。
*注意:jQuery 3。^引导4。^
我在Angular中的解决方案
// push history state when a dialog is opened
dialogRef.afterOpened.subscribe((ref: MatDialogRef<any, any>) => {
window.history.pushState(ref.id, '');
// pop from history if dialog has not been closed with back button, and gurrent state is still ref.id
ref.afterClosed().subscribe(() => {
if (history.state === ref.id) {
window.history.go(-1);
}
});
});
问题:当一个模态打开时,用户点击浏览器的后退按钮,页面导航(向后或向前),但模态仍然打开。
解决方案:
在Javascript中:
$(window).on('popstate', function() {
$('.modal').click();
});
在Angular中:
ngAfterViewInit() {
$(window).on('popstate', function() {
$('.modal').click();
});}
我不喜欢hash。url
中没有哈希值的代码 onModalMounted() => {
if (isMobile) {
history.pushState('modalShow', "", path); // add history point
window.onpopstate = (e) => {
e.stopPropagation()
e.preventDefault()
closeModalFn() // your func close modal
history.replaceState('', "", path);
window.onpopstate = () => {} // stop event listener window.onpopstate
}
} else {
window.onpopstate = (e) => {
e.stopPropagation()
e.preventDefault()
closeModalFn() // your func close modal
window.onpopstate = () => {} // stop event listener window.onpopstate
}
}
}
我要这样做:
// 2.0 and above
@Override
public void onBackPressed(evt) {
evt.preventDefault();
closeTab();
}
// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
evt.preventDefault();
closeTab();
return true;
}
return super.onKeyDown(keyCode, event);
}
->这些是处理程序,需要addEventListener =)
我是凭记忆完成的,当我在我的'code mess'文件夹中找到它时,我会检查它
- 当使用ajax并将html数据保存为对象时,收听浏览器返回按钮.好的或坏的
- 返回按钮代码段的Jquery冲突
- 使用Javascript或AngularJS控制或禁用浏览器返回按钮
- 浏览器返回按钮上的确认框
- 检测用户是否通过浏览器返回按钮到达当前页面
- Safari浏览器返回按钮问题
- 单击“如何检测返回”按钮
- Web浏览器返回按钮应返回到特定页面
- AngularJS的后退/返回按钮
- Phonegap-返回按钮重新加载整个页面
- 如何实现动态生成html的返回按钮
- 当涉及iframe时,获取返回按钮以处理父页
- 一旦用户登录JSF页面,就禁用浏览器返回按钮
- 返回按钮回调函数在特定情况下失败或未启动
- 刷新所有信息返回按钮按下:RubyonRails
- 加载相同的结果返回按钮
- 平滑滚动并使用 Firefox 上的 popState 返回按钮 - 需要单击两次
- 用户注销后显示登录,然后按返回按钮
- Chrome 返回按钮会意外终止上传流
- 检测用户何时按下返回按钮