Does AJAX work that way? - html

I recently got a situation where I need to set a bit in a database from 1 to 0 on web page on close event and I found there is no such event but an unload event so I thought I can generate an AJAX call from it.
Now, when some one tries to close the browser, it should be closed instantly because of UE and if I will have AJAX request that will change a bit from 1 to 0 in database, it would probably be taking a second in my internet connection but some one on the other side can have slow connection and the browser will wait little longer prior to close. Am I right? Or the browser will be hidden and it will carry the AJAX request in the background?
Or if you think any other solution is available that would be helpful.

That is not a reliable way to do this. It's not guaranteed that the browser will make the request and it can also potentially detrimental to user experience.
Here's a page with details on better ways to accomplish this:
http://ajaxpatterns.org/Heartbeat

You could try popping up an alert() dialogue after firing off the AJAX event. That'd keep the browser open until the user could acknowledge the alert. But other than that, there's nothing you can do to prevent the browser from shutting down before the AJAX goes through (or fires off at all).

Related

Honeypot: Close previous tab after after hidden link initiates target=_blank?

im trying to build a nice blackhoneyholepotdoom and something that annoys me is the fact bots are able to go back and/or switch tabs. Their behavior is: 1) hit login link, fail, go back. 2) hit register link, fail, go back. 3) Either leave or proceed to try again. I assume this is Xr*m*r behavior? I need to interrupt the "go back" as much as possible to make bot hell on earth.
Besides JS, is there any method ya'll know of to open a link in a new tab/window whilst destroying the previous, or all the others? It could be a popup, iframe trigga top, whatever -- to my knowledge, bots dont care and never get past your header/content anyways.
This has terrible dubious implications if used on humans, hence it doesnt seem to exist -- but figured maybe there was a workaround out there. You can email or spam me at dhaupin at gmail dawt com if you dont wanna share here.
Thank you!

Dartlang: How to detect that the client is closing

I need to be able to detect that the HTTP client page is closing so I may tell the server that it should do some clean up. Is it possible to do this?
You can use the BeforeUnload event to detect when a user navigates away from the page:
window.onBeforeUnload.listen((e) {
// Do what you need to do
});
Note that this will detect any navigation away from the page, not just closing, which I assume is your desired behaviour.
There is no guarantee that the script at the client side will be executed correctly, because user can lose connection, browser can crash and so on. If it is a critical task(cleaning) then in addition to the client side solution you also should use some sort of a timeout.

Detect Javascript being disabled *after* page load

I have come across several posts about how to handle Javascript being disabled while/before the page is being loaded. However, is there any way to detect Javascript being disabled by the user after the page has loaded in order to hide content at the last minute?
Use a dead man switch on setInterval. If it doesn't trip the trigger, then you need to hide that content before the no javascript gremlins destroys it. How can you hide it without javascript? Have a competitive process going between CSS animations, and Javascript. Such that if javascript is not present the CSS animations (to hide the content), win out. And lo, the content waseth hiddeneth.
Attempt to an send an HTTP request to your server? If that ping doesn't arrive, javascript could be disabled.
Use a noscript tag to inform the user of various things in the event that javascript is disabled, such as the fact that the user has javascript disabled
Put a form in a noscript tag to ask a user to tell you that javascript is disabled, or otherwise to send feedback about their browsing experience to your site without javascript
Check if the UserAgent header contains Lynx?
You can find out if a particular user had disabled JS on his browser by using cookies. You can assume that your JS sets a cookie with some key-value pair like js-enabled=true
When the page gets loaded next time, if cookie does not contain js-enabled key then you know that JS is disabled on the page and server returns the content accordingly.
Now, your question:
After the page has loaded in order to hide content at the last minute?
Say even if you know JS is disabled on the browser, how will you hide the content. You again need JS to be executed to manipulate the DOM which is not possible I think when JS is disabled. So one of the solutions can be when your server knows that JS is disabled then return the page which does not have that content which you want to hide
There is a way to show a warning when javascript is disabled before loading: Write a div element with a warning that jscript is disabled and hide it with jscript while loading the webpage. It is not possible to show a warning when javascript is disabled, because javascript is needed to change any part of a webpage.
if your app can stand the hit of doing log checks - I would, say every minute or so, use ajax to call
the backend - which will then log a time. if the backend checks and the last log time is substantially off - then shut down the app.

Chrome Extension - Chrome.windows.onFocusChanged Behavior

I'm trying to make an extension for Google chrome which requires me to be able to identify the currently selected tab. I did this with the chrome.tabs.onSelectionChanged method, however when I switch windows this isn't fired. I plan to use chrome.windows.onFocusChanged to detect when the window changes then use the chrome.tabs.getSelected method. However the problem is that chrome.windows.onFocusChanged seems to be fired more than once. If I'm not mistaken, it returns window -1, then the first window created (usually 1), then the current window. If the first window is selected then it's fires -1, then 1.
Am I using the right method here? Is there a better way of doing this? If I stick with it I might need to keep track of how window changes which is a bit messy.
Kinda worked on my own solution for this. For anyone interested in doing something similar, what I did instead was to use the onFocusChanged as an indicator that there is a window change happening which then starts a requestListener. Using content scripts, I sent a request to the extension whenever there was a window.focus event indicating that the focus is already on that window. The requestlistener then just removes itself. Unfortunately this approach requires all tabs to send requests every time they get focus. Some more tweaking to fix that I guess but for the mean time I think that suffices since sending requests every time there is a change of focus doesn't seem to eat up that much resources.

Before the html tab is closed : how to notify the Flex app inside?

In Firefox (or IE or whatever), when the tab is closed, I remember you could be notified of this, and I'd like to notify my Flex app and tell it to close (whenever it's possible) all the connexions that are opened.
Does anybody know the way to do this?
You have to listen to the onbeforeunload event from the browser.
See here for more details : http://seanmonahan.org/2009/03/19/preventing-users-from-accidently-navigating-away-from-your-flex-app/
Generally you have no control over when tab is closed and when flash/flex application is destroyed. So you should not attempt to take any actions on such event, since they may be easily preempted and your deinitialization logic would be disrupted.
In your case (closing connections) you should note that all connections are closed automatically upon application destruction by Flash Player.
Edit: Actually there is a way to catch page unload event in some cases (see the other answer). But there is no guarantee that such methods will always work (i.e. they may not work on browser application close), therefore you still should not completely rely on them.