开发者

How to implement onbeforeunload behavior for a div?

开发者 https://www.devze.com 2023-03-12 15:57 出处:网络
I posted recently regarding onbeforeunload implementation. Its working now but that only solved half of the problem. My application has lot of tabs and I want to throw this same confirmation to user i

I posted recently regarding onbeforeunload implementation. Its working now but that only solved half of the problem. My application has lot of tabs and I want to throw this same confirmation to user if he clicks on a new tab without saving changes. all the tabs are div elements. How do you implement something of this sort? Sa开发者_JAVA技巧mple tab structure is attached, where every tab loads a new page.

How to implement onbeforeunload behavior for a div?


Something like this would help you. You need to add the clickHandler to each tab click event.

var dirty = false;

function clickHandler(){
     if (dirty && confirm('you are leaving the tab, wanna stay and save?')){
         return false; //prevent default
     }
     else {
         dirty = false;
         //trigger your tab load
     }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消