29

We are using JS remoting. Found this interesting race condition:

  1. JS Remoting AJAX request made by user.
  2. Before it completes the user moves to another page
  3. When user moves browser to another page the js remoting ajax request is aborted.
  4. The abort invokes the error handler and we have a generic error handler which pops up a dialog showing exception info.
  5. 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 that error message is also used when a JS remoting ajax request is aborted because of network failure in the middle of an ajax request - not just because it is aborted because of navigation to another page. If you want different error handling for the two it is impossible to differentiate between them.

Note I have to use JS Remoting here. Using forceTK etc is not an option. So I am wondering has anyone else seen this and if so what is your solution?

Thanks

  • Have you tried a "native" window.onunload=function(){ SomeJavaScriptCode }; javascript function? I have the problems with for example jQuery(document).ready() function. Then i've tried the window.onload iand it works in realy all cases. – Sergej Utko Jul 4 '13 at 10:50
  • I can confirm that I've run into this problem as well, and also have yet to find a solution. – damien Aug 2 '13 at 18:24
  • Can you share the generic exception handler youve written? – Phil Rymek Dec 4 '13 at 20:16
1

Recently I have found out one problem in JS remoting in salesforce is that It doesnot work with the Enter key. For eg. If you hit any key apart from enter My JS script is calling. On every Event it is working but when I it enter it does not work. Then to verify the same I have put if statements to catch the key value == 13(Enter key Ascii code) to verify if it is Enter key issue only.

TO my observation I am correct the issue persists with enter keyevent. Normally JS remoting will listen until the response is returned from the apex class. But on hitting enter key it escapes every thing and just executes the complete code and exit. It will not at all listen for the response. Infact I have seen in the logs with enter key it is skipping the JS remoting call.

Please check if same is the case with your event your are trying.

  • You can use event.preventDefault() if you want to keep Enter from submitting the form (and thus cancelling your AJAX request). – sfdcfox Oct 22 '13 at 14:21
0

Are the error handler invocation and the unload event are fired at almost the same time?

If so, you could add a css or js animation to introduce a slight loading delay to the error dialog and then hide it when the unload event fires.

If you still need a hand, it would be much easier to help you if you create a minimal example and post the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.