<script>
//이 페이지에서 뒤로가기 하거나, 목록을 누를때 그리고 새로고침을 할 때 이벤트 발생
window.onbeforeunload = function(){
return '페이지를 벗어나시겠습니까?';
}
</script>
ex2
<script>
window.onbeforeunload = function() {
jQuery('.portfolio_nav5 li:nth-child(1) > a').click();
}
</script>
ex3
$(window).on("beforeunload", function() {
return 'Dialog text here2.';
});
- 출처 : https://jhgan.tistory.com/27 [간둥이]
ex4
jQUery(window).bind("beforeunload", function (e){
return "창을 닫으시겠습니까?";
});
ex5 : 페이지 이탈시 확인창 띄우기 I
<script>
var checkUnload = true;
jQuery(window).on("beforeunload", function() {
if(checkUnload) return "이 페이지를 벗어나면 작성된 내용은 저장되지 않습니다.";
});
//단, 글쓰기 버튼을 클릭해서 글을 저장한 후 페이지를 이동시 메시지가 뜨지않도록 checkUnload를 false로 변경 후 submit이나 페이지를 이동
jQuery("#saveBtn").on("click", function(){
checkUnload = false;
jQuery("#saveForm").submit();
});
</script>
- 출처: https://stove99.tistory.com/128 [스토브 훌로구]
ex6 : 페이지 이탈시 확인창 띄우기 II
jQuery(document).ready(function(jQuery) {
var checkload = true;
//글을 모두 작성하고 등록할 때 알림문구가 뜨지않도록 #submit-btn을 클릭시 checkload 변수 값을 false로 만든다. 그러면 조건문을 지나치게되어 알림이 뜨지않게된다.
jQuery('#submit_btn').click(function () {
checkload = false;
});
jQuery(window).on("beforeunload", function () {
if (checkload == true) return "나가시겠습니까?";
});
});
- 출처 : https://blog.tunalabs.io/archives/172498 [TUNALABS.IO 블로그 취미입니다]
ex7 : unload(), 페이지에서 벗어날 때
$(window).unload(function() {
alert('Handler for .unload() called.');
});
- 출처 : https://findfun.tistory.com/329 [즐거움을 찾자 Find Fun!!]
'jQuery' 카테고리의 다른 글
[jQuery] 스크롤값에 따른 anchor offset 적용 (0) | 2019.05.01 |
---|---|
[jQuery] 현재 스크롤값 가져오기(scroll) (0) | 2019.05.01 |
[jQuery] $(document).ready() vs window.onload (0) | 2019.04.29 |
[jQuery] How to smooth scroll to an id after clicking a link from another page (0) | 2019.04.27 |
[jQuery] lang() Selector (0) | 2019.04.27 |