How to know when window is opening first time? - javascript

I have a modal dialog window. Is there any event (or way) how to know when window is opened first time (not refresh, not postBack etc)? It is necessary for clearing cookie.

I assume you are the one coding the 'opening window' behavior. Why not store some variable (flag) that indicated whether the windows has been opened before or not? This can be a normal veriable or a session, cookie, local storage, etc. for long term storage.

If the just the cookie is your concern,
then change the cookie key(if you want to discard old cookie) or
don't set any expiration time for you cookie(if you want it not be sent with next fresh request) as default is session

Related

How to use sessionStorage for a webpage for a login user?

I have been using sessionStorage for a little bit now. As I came to know sessionStorage persist data till the window or tab is open. One we close the tab, sesstionStorage clears all data and create when new session is created.
Now my Question is How to clear sessionStorage for a particular session in which user logged in and logged out?
As per my investigation I think if we make a AJAX call to get the current user'id and at the time of log out we issue "sessionStorage.clear()" it should clear all the data persistance. Is there any other way to do this.
Note: sessionStorage should cleared out automatically when User logged out from application. Before downvoting, answer the question or just go back to your condo.
when you login
//Argument 1 and 2 both have to be strings
sessionStorage.setItem('login','{username: foo, other:sessionData,whatever:bar}')
when you logout
sessionStorage.removeItem('login')
Where login is just a name, you can call it whatever you want

window:beforeunload executed on page refresh in Angular 5

I want to clear some localStorage variables when browser close or browser tab close.
It's working totally fine but when I refresh the page it will also clear localstorage variables.
So, How can we differentiate the page refresh and browser close event in Angular-5 ?.
My .ts file code,
import { Renderer, OnInit, AfterViewInit,ElementRef,HostListener} from '#angular/core';
#HostListener('window:beforeunload', ['$event'])
beforeunloadHandler(event) {
localStorage.removeItem('rememberMe');
}
This looks like an appropriate use case for sessionStorage.
https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
The sessionStorage property allows you to access a session Storage object for the current origin. sessionStorage is similar to Window.localStorage; the only difference is while data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.

Is there any way to detect if the browsers cache is cleared

Is there any way to detect if the browsers cache is cleared , I want to check this on a javascript application and I am using session storage.
Example:
I am on page 1.
i do some operations, like edit a field ..etc
i clear the cache.
Can i get to know that cache is cleared , when i am still on page 1. i use session storage.
I understand , the storage event will let us identify any modification on the session storage when the current tab is closed and new tab is opened.
but i want to set a flag or throw an alert message whenever the cache of the current browser is cleared.
Please help!
I dont know straight way, but this may help:
if (!localStorage.length && !sessionStorage.length) {
// No items in localStorage and in sessionStorage,
// cache possibly cleared
[your code here]
}

Execute JavaScript code once a browser window/tab is opened for the first time

Is there a way to execute JavaScript code only once when a window or a tab in a browser is opened and then never again for the whole life-cycle of this window/tab (even when navigated away)?
One way to use window.sessionStorage like this.
if (!window.sessionStorage.getItem("isExecuted")) {
//execute code
window.sessionStorage.setItem("isExecuted", true);
}
MDN docs
The sessionStorage property allows you to access a session Storage
object. sessionStorage is similar to Window.localStorage, the only
difference is while data stored in localStorage has no expiration set,
data stored in sessionStorage gets cleared when the page session ends.
A page session lasts for as long as the browser is open and survives
over page reloads and restores. Opening a page in a new tab or window
will cause a new session to be initiated, which differs from how
session cookies work.
Here is a solution based on checking the window.history.length is 1 (i.e. the first time they've been to this window/tab). It also does a check that they've not simply refreshed the page.
if(window.history.length === 1 && performance.navigation.type !== 1){
alert("show me once per tab");
}
If you put the script you want to run once where I have my alert, it should work for what you are trying to do.
You can use localStorage.
Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.
The localStorage object stores the data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year.
localStorage.getItem('key');
localStorage.setItem('key', 'value');
Doc: http://www.w3schools.com/html/html5_webstorage.asp

ASP(classic)/IIS 7.5 - Can you check if Session has timed out without refreshing the session?

I'm working on an ASP classic application running on IIS 7.5. I am attempting to throw a popup overlay over the user's window if their session has timed out without them ever having to interact with the machine (for instance if they walk away from the machine and forget about it, I want this popup to be waiting for them when they get back).
I have implemented a background setInverval function that uses jQuery to send an ajax request to a page on my server named ajax-check-session.asp.
ajax-check-session.asp does nothing but check if the session still exists or not, and Response.Write a 1 or 0 respectively.
The session checking works, I can log out from a different tab and the pop-up will show up in any other tabs that were using that session. However, if I just leave the tab alone for 20 minutes (the Session.Timeout value), the pop-up never shows up. I'm assuming this is because the ajax request to ajax-check-session.asp is refreshing the session every 3 seconds so that it will never time out.
Here's the code for ajax-check-session.asp:
<%Option Explicit
Response.Write LoggedIn()
Function LoggedIn
Dim strUsername
strUsername = Session("username")
If strUsername = "" Or isNull(strUsername) Then
LoggedIn = 0
Else
LoggedIn = 1
End If
End Function
%>
Does anyone know if accessing this page every 3 seconds is, in fact, the reason my session won't time out? If so, do you have any suggestions for alternative techniques to accomplish my goal, or a fix to my code/server that will allow this page to not refresh session?
I have attempted using <%#EnableSessionState=False%> on this page, but I can't think of a way to still check if the session has expired with this set to false, as this seems to revoke access to Session entirely.
it's correct when you calling Session("username") in fact you are refreshing the Session Object itself, because that you will never see the popup.
an alternative is to use the Global.ASA file to implement a the functions Session_OnStart and Session_OnEnd to store the SessionID and the date/time when it was created in a database, and your script can check the records.
you can use both function to insert and delete the session info from the table.
Global.Asa Sintax (MSDN)

Categories

Resources