window.onbeforeunload在javascript中 [英] window.onbeforeunload in javascript

查看:169
本文介绍了window.onbeforeunload在javascript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用javascript window.onbeforeunload函数在用户尝试离开页面而不提交数据时向用户显示提醒。

I'm using javascript window.onbeforeunload function to show an alert to user whenever user tries to leave the page without submitting the data. This functinality is working fine.

问题出现在IE(仅限),其中window.onbeforeunload事件即使点击

Problems comes in IE (only) where the window.onbeforeunload event fires even on click of internal links like

<a class="editlink" onclick="fnEditLink(this);" href="javascript:void(0);" > <img src="/images/newstyle/pencil.png" alt="edit" /> Edit </a>

这是一个内部链接,用于编辑网格行而不进行任何重定向或ajax调用。

This is an internal link that is used to edit grid row without making any redirection or ajax call. Just a plain simple edit of newly created row in html DOM structure.

这是我在页面中使用的JavaScript代码:

This is the javascript code that I've used in the page:

<script>
var is_clicked = false;
$(document).ready(function(){
    $('#get-a-quote-step2').submit(function(){
        is_clicked = true;
        return validate('get-a-quote-step2');
    });
    $('#skip_step, form input').click(function(){is_clicked = true;});
});

window.onbeforeunload = function() {

    if(!is_clicked)
        return false;
}
is_clicked = false;
<script>

一切都在firefox工作正常,但在IE它正在创建问题。
我使用IE 8测试这个东西,但我想问题是与alll版本的IE。

Everything is working fine on firefox but on IE it is creating problem. I'm using IE 8 for testing this stuff, but I guess problem is with alll versions of IE.

在我上面的代码,问题是如果我点击一个内部链接,它将is_clicked标志设置为true,然后如果我按刷新按钮,我可以重新加载页面,而不询问用户他是否会丢失所有未保存的数据。

In my above code, problem is if I click on an internal link it sets is_clicked flag to true and then if I press refresh button, I'm able to reload the page without asking user that is he leaves he will loose all unsaved data.

我在想是否还有esp;在JQuery中,这样我们知道如果一些特定的链接没有被点击,像isNotClicked()?

I was thinking if there is anyway esp; in JQuery so that we know if some particular links are not clicked some thing like isNotClicked() ? Or if there is any other solution to my problem it is most welcome.

推荐答案

所以如果你只是想防止某些链接发布onbeforeunload事件你可以简单的返回false在你的点击事件声明...

So if you just want to prevent certain links from firing the onbeforeunload event you can simple return false inside of your click event declaration...

<a href="http://google.com">Call unload first and load a page</a>
<a href="javascript:void(0);" id="request">Do an ajax request</a>
<script>
$(document).ready(function(){
    $('#request').click(function(e){
        //do some stuff
        //returning false wil prevent the onbeforeunload event from firing
        //but also will not redirect if the link has an href
        return false;
    });
});
window.onbeforeunload = function(e){
        return 'Want to leave?';
}

</script>

这篇关于window.onbeforeunload在javascript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆