We are using JS remoting. Found this interesting race condition:
- JS Remoting AJAX request made by user.
- Before it completes the user moves to another page
- When user moves browser to another page the js remoting ajax request is aborted.
- The abort invokes the error handler and we have a generic error handler which pops up a dialog showing exception info.
- In some cases, the dialog will flash up for a second because the page navigation away hasn't completed.
This looks ugly and we would prefer if the error handler knew there was a page unload happening and not show the error dialog if this was happening.
So, I tried adding a page unload listener and setting a flag in that which can be subsequently checked.
jQuery(window).unload(function() { unloadhappening = true; });
This approach will work in most ajax designs but won't with JS remoting. JS remoting, calls the error handler before the unload event. Not sure why?
I tried the earlier beforeunload event:
jQuery(window).on('beforeunload', function() { unloadhappening = true; return ''; });
This won't work because this event just shows up another dialog.
So I tried to add a listener to all anchor tags that have a http(s) hyperlink
jQuery('a[href^=http]').click( function() { unloadhappening = true; })
This won't work for relative URLs.
So I tried to adding a listener to all anchor tags that have a href
jQuery('a[href]').click( function() { unloadhappening = true; })
This will filter out components that won't work because it won't filter out components that don't trigger a page unload such as:
<a href="#adviceTab">Advice</a>
So before I go and write a selector to filter out anything with a fragment identifier, there are also some anchors that are coming form JQuery UI that do:
<a href="javascript:%20void%280%29%3B" class="calToday" onclick="DatePicker.datePicker.selectDate('today');return false;">Today</a>
I am starting to think this is a more of a hack that will never work as opposed to a solution. Do you have a good idea for this problem?
The only idea I can think of is to check the exception, error, event info returned by JS remoting for an aborted request. I see: Strings:
Unable to connect with Server
and
Communication Failure
But they might be also used in another error that has nothing to do with a page unload. So wondering has anyone else seen this and if so what is your solution?
Note I have to use JS Remoting. Using forceTK etc is not an option.
Thanks