I am using a bookmarklet to inject an iframe into another website, and am trying to post data back to my server when any forms on the page are submitted. Unfortunately, the AJAX request inside the iframe doesn’t always complete before the browser drops all connections as part of navigation. How can I reliably send data back to my server during the onsubmit?
I’ve attempted:
- onbeforeunload – isn’t firing after the form’s onsubmit event
- onbeforeunload seems to prevent background javascript events while it’s active
- Making the AJAX request synchronous – did not help, possibly because the request starts during the unload itself
- onunload – does fire, after the request, but doesn’t seem to have any authority to slow navigation for the AJAX
I am worried about trying to:
- break the form submission, as there might be javascript run on submission
- create a browser extension, due to unknown risks and possible schedule overrun
- directly run the request from the parent – the request is part of an in house library running on top of JQuery, and hacking around it would be fragile
In all cases the browser that I am targeting is Google Chrome (latest release).
I ended up spawning popunder windows instead of using AJAX requests, and pulling my data from GET parameters. The response to the browser is a HTML page with a script tag in the head containing
window.close(). This seems to be working for the time being.