To put you in the picture :
- A user is on a mobile device and is looking at a browser (i.e. Safari on an iPhone)
- They can see a password field to create a password in a modal box
- If the user were to close the modal box and e out of the browser I would like to send them an email to prompt them to create a password.
My initial thought is there must be some sort of java-script event to be able to do this ?
So can you detect the following user actions with JavaScript where a users has : - Closed a Tab - Closed their Browser - Left their browser
To put you in the picture :
- A user is on a mobile device and is looking at a browser (i.e. Safari on an iPhone)
- They can see a password field to create a password in a modal box
- If the user were to close the modal box and e out of the browser I would like to send them an email to prompt them to create a password.
My initial thought is there must be some sort of java-script event to be able to do this ?
So can you detect the following user actions with JavaScript where a users has : - Closed a Tab - Closed their Browser - Left their browser
Share Improve this question edited Jul 8, 2015 at 10:36 Kiquenet 15.1k36 gold badges151 silver badges247 bronze badges asked Feb 9, 2015 at 11:16 Dominic FrancisDominic Francis 4195 silver badges8 bronze badges 4- 1 This perhaps? developer.mozilla/en-US/docs/Web/API/… – Evan Knowles Commented Feb 9, 2015 at 11:18
- These kind of "automatic annoyer" should be rethought anyway ... – Axel Amthor Commented Feb 9, 2015 at 11:20
- You can always set a cookie in the user's browser that records if they have signed up or not. – kontur Commented Feb 9, 2015 at 11:22
- Agreed, we actually do want to put a cookie on a user's machine if they don't submit a password. However its a phase 2 item. – Dominic Francis Commented Feb 9, 2015 at 15:49
4 Answers
Reset to default 3Also looking for the problem, but that you will not satisfy demand.
I had just found how to solve this problem: "Can you use JavaScript to detect whether a user has closed a browser tab? closed a browser? or has left a browser?"
<html>
<head>
<title>Detecting browser close</title>
<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
var validNavigation = false;
function wireUpEvents() {
var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
var leave_message = 'ServerThemes.Net Remend BEST WEB HOSTING at new tab window. Good things will e to you'
function goodbye(e) {
if (!validNavigation) {
window.open("http://serverthemes/best-web-hosting-services","_blank");
return leave_message;
}
}
window.onbeforeunload=goodbye;
// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function(e) {
if (e.keyCode == 116){
validNavigation = true;
}
});
// Attach the event click for all links in the page
$("a").bind("click", function() {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function() {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
wireUpEvents();
});
</script>
</head>
<body>
<p>
Check which action detects browser window close:
<ol>
<li>Click this <a href="#" onclick="location.reload();return false">Refresh</a> link or the browser's Refresh button</li>
<li>Navigate away from this page through a <a href="http://serverthemes/">link</a></li>
<li>Type another URL in the address bar</li>
<li>Click Back or Forward button</li>
<li>Click the Close (X) button in the top-rightmost corner of the browser</li>
<li>Click the IE icon in the top-leftmost corner and choose Close. Or simply double-click the icon</li>
<li>Press Alt+F4 key</li>
</ol>
</p>
<p>In IE, the last 3 actions are correctly detected by the <a href="#" onclick="alert(doUnload);return false">javascript code</a> inside this page as browser close.</p>
</body>
</html>
If your user closes a tab, you may detect it with the onunload event, but when she closes the browser, your javascript engine will abort as well, so you can no longer run any type of program on the client side.
You could, however, detect it on the server. If you prepare a websocket connection, for instance, you may detect when your client has quit and then do your desired action.
The process would be something like this: on entering the page, set up an automatic websocket connection to your server. Read the email if needed once it is entered (you may also send it via a websocket event to the server) and finally, when the "disconnect" event fires, send the email to that address.
If you would like to proceed in this way, you may want to look at the Socket.IO webpage, which is to me one of the easiest ways to implement this process.
Short answer is not really, no. Long answer is there is an onunload
event, but all you can do with it is ask the user if they're sure they want to leave (see @EvanKnowles's ment) but you can't attach any plex logic to it.
The best way is to have some sort of session timer that will send an email N minutes after the last time they were seen on your site.
Document for you:
http://www.openjs./scripts/events/keyboard_shortcuts/
shortcut.add("Ctrl+Shift+X",function() {
alert("Hi there!");
});
Download: http://www.openjs./scripts/events/keyboard_shortcuts/shortcut.js