detect completely browser closed in JavaScript - javascript

I am working on a project using polymer 3 where I want to destroy/clear a cookie and log out the user from the website. Destroying or clearing cookie is not an issue but I am just not able to detect when the browser closes.
So far I have tried:
window.onbeforeunload
from the JavaScript end and for php have tried this:
session_set_cookie_params(0);
but none are working. Any help would be really appreciable.
I can use setInterval() or setTimeout() function but it's not fulfilling my requirement.
Plz don't suggest any jQuery suggestion.

Use sessionStorage. It Works propably exactly as you want. Whenever user close browser, everything in sessionStorage is removed. Be aware that it works only when user completely close browser. Not only a tab.

I would go the other way around. If the user arrives to the page for the first time, log out the user. You can easily do that by checking if a certain variable is set or not, and then set it whenever the user logs in.

Related

Disable refresh icon of chrome

Can we disable the refresh icon of Chrome browser using jQuery/JavaScript? I was able to disable refresh using F5 but not using the refresh icon. Any help is appreciated.
Short answer: No.
Long answer: No, because allowing websites to disable browser controls could cause all sorts of bad consequences and allow hijacking the browser or holding it hostage. The best you can do is prompt the user and confirm their action, as indicated in this answer.
Also, FYI, it is a refresh button, not an icon.
You can't disable refresh action of a browser. It's impossible.
If you need to remember something important you can use COOKIES or LocalStorage|SessionStorage to save data.
To truly disable refresh you must use a server side script to check for page state changes
Try using a server-set cookie, such as PHP's $_SESSION, or even .Net's Response.Cookies, to maintain "where" your client is in your site.

Google Chrome isn't saving Javascript on navigation

Hi I know that there's a question about this already, but all of the answers don't seem to be working for me.I'm trying to make the JS console not reset on navigation.I know there's the "Preserve log upon navigation" checkbox/button, but it's not working for me.Whatever I entered is cleared and the page is loaded as default. How can I make it save what I changed, like a kind of add-on? (preferably without installing any additional tools)..Or am I misunderstanding the use of the preserve log upon navigation thing? Anyway, how can I achieve what I'm hoping to?Thanks in advance!
The "Preserve log upon navigation" setting doesn't store any changes you've made to the DOM using the console, it only preserves the log of what has been output to the console.
As far as I know there is no way to preserve the changes you've made via the console across links/refreshes since you are always loading/reloading the page. I doubt there are even add-ons to preserve that kind of thing.
As winterblood stated in his answer, there's no way to preserve the changes made to any js file or the dom after a page refresh.
What you CAN preserve however are the breakpoints in the code. So if you need to place a change very early on, you can search the first script to be loaded, put a breakpoint in it, and refresh the page. When the page refreshes, then you can make any changes on it that you wish.
Another solution is to use a proxy like charles or fiddler, and do a "map local" of a local js file with your changes in it. See this link for that. I do this a lot in my work. It's dead easy to copy/paste a file from the chrome console into a local file, then use charles to do a map local of the request for that file and reload the page. If everything goes well, you will be able to do any changes you want to that file without modifying anything else in the page.

How to get TAB event in Internet Explorer 8 using JavaScript

I have an application which had developed according to IE6.
Now Because of IE8 tabbing features not the application have been start giving problem.
The Problem is that,
if I have opened first context in "tab1" and then open second context in "tab2".
Now when I back to "tab1" and refresh the page then it loads the second context that is new one.
This is logically correct but I want to block the user while opening the second tab so that user restrict to only first tab.
So I have been come to conclusion that we can do this with JavaScript by getting some tab event, but I am not too much familiar with JavaScript.
Please tell me is there any solution if you have in your mind for above problem,
or tell me is there any way so that we can catch the tab event.
Praise all things glorious: There is no way to prevent a user from opening a new tab. In Firefox, you can cruelly prevent a user from using keyboard shortcuts to open a new tab by returning false from an input element's onkeypress event, but it's still possible to open a new tab otherwise.
The solution? Well, from what you're saying, it sounds as though you're using the session to store the user's current page (and, well, most everything else, but that's another story). Having spent a lot of time using a web application that was built that way, I can tell you firsthand: don't do this! The web already has a wonderful method for storing the user's current page: the URL.

Javascript: Alert user without using an alert box

I'm trying to find an elegant way to inform a user that s/he is about to be logged out and I know that most browsers will give you some indication that a hidden tab has an alert box open. I would like to duplicate this functionality without actually showing an alert box.
I have thought about forcing the tab/window to gain focus, but that is quite obtrusive and I hate it when websites do that to me, so I'm looking for something a little more subtle.
Any ideas?
Edit/Clarification: I already have a div that pops up if they are about to be logged out. My problem is that if they are on another tab, they won't be able to see that div, so I would like some way to notify the user that something important has happened on my tab so they go check it out and see the logout notice.
The favicon idea listed below is an excellent idea, any others?
Here's an interesting way that comes to mind. When its time to be logged out, change the website's favicon dynamically. Newer browsers should be ok with it.
Look here: Changing website favicon dynamically
Some techniques I've seen:
Some sort of sound that's played (I think it's done with Flash in the case I'm thinking of, but maybe it's possible with HTML5's audio tag)
Flashing/alternating favicon
Use JavaScript to change the page title tag every 2s or so
You could create a page that informs them they will be logged out in a certain amount of time with a button that would allow them to maintain their session. Or maybe you could use a lightbox modal popup window (example here).
Why not swap out a div styled how you want to change to let them know they will be logged out soon? Then, you can simply have it as a portion of your page with all the same style and formatting?
For example, your normal page has some sort of page element with visibility:block; and then before they will be logged out, you change that to visibility:none; and change your other element (in the same place) to have visibility:block;
Does this idea make sense? You have to be able to detect when this is happening with Javascript already to alert, so instead of altering you are just swapping out display elements.
I hope this is helpful,
-Brian J. Stinar-
it probably doesn't go with what you're after but a simple modal window is probably a good idea? i know it doesn't alert the user instantly, and they won't see it unless they switch back to that tab, but it's unobtrusive and i believe most users would prefer not to have something rammed in their face!
If this notification is to be triggered by a user clicking "log out" or the likes then they will see it and it won't be as intrusive as forcing them to stop what they are doing and close the alert box.
And if it's due to time out or something similar then the user isn't overly concedrned or they would still be on that tab.
I think that this serves the best purpose in terms of usability as people don't want to be hassled or have their workflow broken by an alert shoved at them! A perfect example is Microsoft TFS which would constantly throw alerts at you when you got signed out, which got really frustrating, really quickly
so my answer is think how the user would like to be notified in the least obtrusive way :-)

How do I know when the browser is being closed?

I am writing an ASP.NET application that tracks the user's scores and info (it is a
training application) to an access database, if any one closes the browser directly I want to display an alert message.
My problem is that I cannot use the unload event because When I pressed any ASP.NET button at that time unload event occurs. What event should I be using that will work for IE and FF?
I want to handle event Browser close(X).
The onbeforeunload event will only give you the confirm box with a message. You can't put more functionality into it.
What you should be doing is having both the window unload eventhandler and the logout button click eventhandler call the same logout method. You don't need to stop the user and ask them to press your button, your button should be just another way of doing the same thing.
Also: have you considered SCORM?
Not sure you know what you want to do here. ;-)
Do you want to prevent the user completely from closing the browser until a certain point in the trainig? If so, have you considered using a modal and maximized window? You should be able to maximize the window without the titlebar, buttons and menus.
Or if you just want to keep track of the progress, scores or similar, then you could use cookies handled by the browser via JavaScript. Just bear in mind that this will keep the data on just one machine for the student.
If you just want to warn the user that he is about to leave the training course, you could use onbeforeunload
I think others have adequately answered your specific question, but Tooney raises some good points. To expand on these. Where are you currently maintaining your the state? Are the scores stored in cookies, within a server-side session or do your persist them page by page within the database.
Assuming this isn't a cheap and cheerful solution, then I would suggest you consider persisting results page by page, as it is minimise the loss of information caused by a premature exit (either by design or accident). Of course, you then need a process to cleanup incomplete training session.
You could still use the onbeforeunload function to trap user exits, but personally I don't like UI's that double check users actions.
Good luck.

Categories

Resources