onunload Ajax not always working (Chrome extension) - javascript

I am writing a Chrome extension, and I want to fire a POST submission RIGHT before my client unloads a webpage.
To do this, I tried using both window.onunload and window.beforeunload. When the page unloads, I use jQuery.ajax to make my POST submission. I also include async: false in my request.
With this, my POST triggers successfully for MOST WEBSITES upon unloading. However, there are some websites that just don't seem to be firing the unload function!
Sites that aren't triggering for me include (just out of testing): http://www.yahoo.com and http://www.paulgraham.com/ds.html
I am really stuck. Is there something specific about the sites that aren't firing that I'm not accounting for? What can I do to make my function trigger universally?
Thank you!

Related

onbeforeunload works different on Chrome than it does on Firefox. I am looking for any potential alternatives

I am trying to add a onbeforeunload to my code so it fires a message that is sent to a server. I am encountering the following issue:
On page refresh, the message is sent to the server when using Firefox. It does not send a message to the server when I refresh the page on Google Chrome.
When clicking on links on my site to navigate to different sites, the server gets a message only when using Chrome. Firefox does not send a message when clicking on external links.
I also know that onbeforeunload is supposed to work when the window is closed, you enter a different website and navigate to that. There are some additional cases which also trigger the function. My site does not work with any of these along with the errors mentioned above.
If there a fix to this? If so, what can I do? I have looked around to fixes but I only get fixes where it prompts the user on whether they want to leave the page or not, I do not want that.
Additionally, I tried adding the function call to onclick on my external links but that is not being triggered on Firefox. I am guessing this is due to the amount of time onbeforeunload has before it is cancelled. If so, there a way around this?
Lastly, if this is not possible with onbeforeunload, what are my other options without prompting the user if they want to leave or not.
Thanks in advance!
EDIT: Here is some code that can be used:
https://jsfiddle.net/985drboy/
Make sure, that your code sending the message to the server is blocking and not async. If onbeforeunload returns before your message is sent, you get undefined behavior e.g. your message might not be sent if the page is unloads before the sending was complete. The browser continues unloading as soon as unbeforeunload returns.

Django: How to make an API call just before browser tab close?

I am using Django v1.10 for an application where in need to send an API call just before browser close through crude javascript (don't want to use any library for sake) preferably. I've read about window.unload and window.onbeforeunload. The first one didn't seem to work at all. The second can work but it also gets executed when there is reload or redirection to another page (it works as it should but that is what I don't want). I've tried using SESSION_EXPIRE_AT_BROWSER_CLOSE of Django which works only when a user has totally exit out of the browser (no browser process running). I've also seen answers on the web where people have suggested to open another window/tab of the browser through JS as the tab closes.
So in precise words, I want to make an API call just before browser tab close (not in any other situation).
Please help!

Firefox/IE cached page on startup only shows AJAX content

I have a webapp that uses AJAX calls to load HTML snippets in various places. Note that the webapp also simulates regular navigation with the use of history.pushState function (not sure it has anything to do with my problem but I still mention it).
Problem:
Whenever I exit the browser with a tab displaying my webapp and restart it again, the browser does not trigger a regular HTTP request to load the whole page. It simply uses a cache mechanism to load the response of the latest HTTP request. In my case, if the last request happens to be an AJAX call, then the browser will only load the HTML snippet without css, js nor the rest of the HTML.
Note that the problem appears in Firefox. In IE, the whole page can be reloaded if you accept the execution of scripts when being prompted. Chrome works fine as it loads the entire page.
Question:
Is this a normal behavior and how can I prevent it (i.e. make it work like in Chrome)?
Thanks for helping!

Resend Http Request via JS

I am currently trying to manually develop an unsaved changes warning in our JSF-based Webapplication. Sadly our customer does not like the styling of the default warning displayed via an alert() after using the onbeforeunload event and requires us to develop a similar mechanism on our own.
I was thinking of using the way described here to prevent the onbeforeunload event from displaying the alert() and simply showing my own modal panel. I just can't figure out how to make the "Yes" (yes, I want to leave this page and lose all unsaved changes) button work. The button should basically resend the request which lead to the onbeforeunload event which could have been a HTTP Request as well as an Ajax Request. How would one do that via JS?
Thank you :)
Cheers
//edit: It needs to work in IE9 as well as Chrome 38

Debugging ajax, resend request

This is something that I've been looking for for a while. When I'm debugging my ajax applications I have to keep reloading the whole page, even if the only changes I made were on the server.
For example, in my current application, there is Tinymce being loaded, jQuery UI, I have to click one dialog, to click another to click another. What I would love is the ability in something like Firebug to right click the request and select 'resend' especially when the only change I made is on the server e.g. print_r( $_POST );
The closest I've gotten to this ability is in Firebug on the Net tab, right click the request and click 'open in new tab', which is nearly there - but not quite
You need to replay the HTTP request, thats something firebug isn't meant to do. You need fiddler to get the work done. Fidder2 even has a Firefox addon. You can replay your request using fiddler.

Categories