How to include check
<p>I have a function that checks for a particular event and when it detects it is displays an overlay message. Until that happens I need to have <strong>beforeunload</strong> in place to warn a user about potential data loss in case a they decide to leave the page before the task is complete. I added it in the following manner, but even when the task is complete and the beforeunload should not fire it still gets triggered. I think it is due to the <strong>bind</strong>. Is there another way it can be implemented to achieve what i am trying to accomplish?</p>

<pre><code>if (!$("#area").find('.item').length) { 
    $("#message").dialog({
        modal: true
    });
 } else {
    $(window).bind("beforeunload", function() {
        return "Are you really sure?";
    });
 } 
</code></pre>
