views:

180

answers:

2

Given the following function:

window.onbeforeunload= function() {
    if (CKEDITOR.instances.stuff.getData().length > 0 && oktoquit == false) {
        return "You have unsaved changes. Click Cancel now, then 'Save' to save them. Click OK now to discard them.";
    }
};

I'd like a way to exclude this function from running if the user clicks a link in a div with an ID:

<div id="ignore me"><a href="">blah</a><a href="">blah</a><a href="">blah</a></div>

Any ideas?

** Update Turns out the following code for the site's search is causing the issue. but why?

$("#searchresults li").live('click', function(e) {
    if (e.target.nodeName != "a") {
        window.location = $(this).find('a').attr('href');
    }
});
+1  A: 

You can do this:

$("#ignoreMe").click(function() {
  window.onbeforeunload = null;
});

I changed your ID to be ignoreMe so it's valid, but you get the point :) Judging by your question, you may want #ignoreMe a for a selector, if you want to exclude any links inside that div from executing the handler, whatever you want, just use that selector.

Nick Craver
Very interesting... So would this code go before or after the code I have above??? meaning in addition? Or can the 2 groups of code mine above and yours and combined in a smarter, cleaner way?
AnApprentice
@nobosh - Yes it should be in addition. There's not really a much better way to combine them, just place this code inside a `$(function() { });` so it runs on `document.ready`...the before/after doesn't matter, since the code inside the click handler won't run until a link clicked.
Nick Craver
@nick, I tried the code out, problem is now window.onbeforeunload is never working, the back button etc. doesn't trigger the prompt to warn the user of unsaved changes. I think it be great if it was one function, a function expanded upon what was pasted in the question, but there was an if statement inside, saying if it was clicked, ignore and continue? Does that make sense? any ideas?
AnApprentice
@nobosh - It sounds like you have a javascript syntax error somewhere causing nothing to work, use FireBug or Chrome's console to see what the error is, fixing that should resolve the issue. You could store a variable, but I believe setting the handler is null is much simpler, and is less code ass well.
Nick Craver
I think the issue is that while clicks in the #ignoreMe div cause the bug, when I use JS to report what was clicked it's thinking the entire page?
AnApprentice
@nobosh - That's correct as well, unless stopped, all clicks bubble to their parent, so a click on anything, by default, will bubble up to the `document` itself. Do you have an example page I can look at? If I can view it directly, should be pretty quick to see what's going on.
Nick Craver
Let me put something together now... Thanks Nick
AnApprentice
Incredible, while putting the demo together for the last hr, I was able to isolate the bug due to a seperate item of JS used for the site's search... but don't understand why that would be causing problems here. I've updated the question above with the code casuing the issue. See anything strange with it?
AnApprentice
@nobosh - I don't see anything inherently wrong with that, is it throwing an error or something else?
Nick Craver
No error jsut ckeditor triggering the beforeonload anytime the nav in ckeditor is clicked. I'm guessing it's a ckeditor bug and am checking with them to confirm.
AnApprentice
@Nick, new idea, is there a way to update the orig window.onbeforeunload code above to just work for the top window and not the iframe where ckeditor lives? that might work?
AnApprentice
ok wow, that worked.. After 6hrs figuring this out it only took 4 characters
AnApprentice
A: 

window.top.onbeforeunload this ignores the CKEDITOR iFrame which does the job

AnApprentice