Session lost when click on javascript generate link asp.net - javascript

I am making a website that has a shopping basket feature and I am using the session to store the shopping cart. When I navigate through the site it works fine and displays the number of items in the basket, but when I click on a link that is create by a javascript function it loses the variable on the session. It only causes this problem on the links generated by javascript
Adding item code
if (context.Session["jobBasket"] == null)
{
context.Session.Add("jobBasket", new System.Collections.ArrayList());
}
var list = context.Session["jobBasket"] as System.Collections.ArrayList;
var item = int.Parse(context.Request["jobId"]);
if (!list.Contains(item))
{
list.Add(item);
}

Are sessions even enabled for your app? for instance - if you have a single page that reads, updates, and prints out the value - does it stay? if not your session is potentially disabled or configured odd, check your web.config

Related

Run different js on each user visit

I want to load javascript functions on a wordpress site based on user visit, if it's the first time, then one function will run, if its second visit another code will run., I found that using cookies and local storage we can set it, but only track user if visited or not., I want to count the user visit, and based on that run the functions.
var first_visit = false;
checkFirstVisit();
function checkFirstVisit(){
if(localStorage.getItem('was_visited')){
return;
}
first_visit = true;
localStorage.setItem('was_visited', 1);
}
console.log(first_visit);
this code is to track the first visit, How do I make it to track the number of visits?
You can use session storage in PHP (https://www.w3schools.com/php/php_sessions.asp) to store in a variable whether this is the user's first visit or not.
Then using an if-else statement, display the required code.
The good thing about PHP session storage is that it can retain the storage even after the user closes the browser.
it is the same thing
set a cookie for visiting
if it is not set so it is the first visit so set it to 1
if it is 1 so second visit so set it to 2
if it is n so it is n + 1 visit so incrment n

How to create global constant or variable whose value dont get reset after closing the app in reactjs [duplicate]

I'm trying to make a page on my tumblr that has a button on it, which counts how many times it's been pressed. I've gotten it to work, but a browser close or refresh clears the value, and clicks from different people don't stack. Here's what I have:
<body>
<div class="main">
<h3>Click Counter</h3>
<button id="clickme">Click me: 0</button>
<h5>Filler Text</h5>
</div>
<script>
var button = document.getElementById("clickme"),
count = 0;
button.onclick = function() {
count += 1;
button.innerHTML = "Click me: " + count;
};
</script>
</body>
This makes the button count up when pressed, but I want it to behave like so:
User #1 clicks twice. Counter reads two.
User #2 visits the page, counter is already at two, user clicks 3 times. Counter is at 5.
User #3 visits, counter is still at 5 for them as well, etc.
As of right now, each user visits the page and the value starts at zero. Help?
Depending on your needs, you could use localStorage.
count = localStorage.getItem('count');
count = parseInt(count); // because localstorage stores everything in strings
// First time the value does not exist...
if(count == null) {
count = 0;
}
button.onclick = function() {
count += 1;
button.innerHTML = "Click me: " + count;
localStorage.setItem("count", count);
};
This is not a server side solution. It's also less reliable as it can be cleared by the user anytime.
javascript runs on the browser and store all the variable in the browser so the scope of a variable is in the browser. you can use local storage but if all user uses the same browser which is not your need so only way to persist data is to save count (data) into the server and fetch count (data) on a load of the page.
you can use firebase real-time database if you don't have any server.
Getting started with Firebase real-time database for the web
As I have understant form your question you want to retain the user click count, there is 3 scenerio
User visits form Single machine Single browser (rare situation)
in this you can use local storage
User visits form Single machine multiple browser
In this case you have to save it in server side
User visits form multiple machine multiple browser
In this case you have to save it in server side
Both case 2,3 are same , you need to save it in server.
A common way to do this , put you counter in db and an REST end point and call that endpoint on page each page load to take the counter form the server and also after clicking update on the server by similar end point
Suggesting learning: Client side : AJAX (Jquery) , Server side : REST API , DB (MySql,ets)

Do i have to use Session Storage or LocalStorage : javascript AWS

I have below scenario
Page 1 has a link, when user clicks on it, it gets navigated to portal page with page reload. So just before navigation, a JSON object is created
The size of this object comes around 4KB roughly.
Sample object
let obj = {
"date":"12/31/2018",
"year":"2019",
"zip":"93252",
"members":[
{
"sdf":true,
"age":21,
"fdssss":false,
"aaaa":false,
"fdss":null,
"fsdfsd":[
"ADULT"
]
},
{
"sdf":true,
"age":21,
"fdssss":false,
"aaaa":false,
"fdss":null,
"fsdfsd":[
"ADULT"
]
}
}
There is a back link from that portal page, on clicking page will be navigated back to Page 1 with a page reload.
So when the page is navigated back, I need the created JSON object back again. I need it only for that session or the data should be persistent even if the page is reloaded.
Do I have to use localStorage? If i store the object in localStorage, at what point i should clear the storage? How should I handle between different users?
Do I have to use sessionStorage? what will be the scope of the data availability
I'm using AWS service.
Q1:
you can have localStorage, and you should handle it at the code when first page loaded and you can delete it when user do signout or login, storage is about browser not user, if there are some users behind one computer at different times you must clear all data manually.
Q2:
you can also have sessionStorage, per tab and will be removed by closing browser.
in details:
This is depends on your scenario which means localStorage used for long time but sessionStorage used when you need to store something temporary.
but the important thing about sessionStorage is that it is exist per tab if you close tab and windows the sessionStorage completely removed, it used for critical data such as username and password whereas localStorage is used to shared data whole the browser.
localStorage has no expiration date, and it gets cleared only by code, or clearing the browser cache or locally stored data whereas sessionStorage object stores data only for a session, meaning that the data is stored until the browser (or tab) is closed.
at the end I suggest you to use localStorage because you may want to share that data whole the browser event after closing browser and you can store more data, in the other side there are limitation about them, when you are used storage you should handle them manually and take care.
suppose:
function removeStorage()
{
var obj = localStorage.getItem('obj');
if(obj !== null)
localStorage.removeItem('obj')
}
and in login or logout success action call removeStorage() and in Page1 load have something like below:
var obj = localStorage.getItem('obj');
if(obj !== null)
{
....
//show the obj in label or do what you want with it
...
}

Unable to remove editor via User object returned from .getEditors()

I am developing a web app for work that adds an editor to a spreadsheet and then removes that editor when his/her step is complete.
I am running into an issue where I can't remove the editor via the User object. The User object returned by .getActiveUser() isn't found in the User[] array returned by .getEditors().
Example:
var editors = SpreadsheetApp.getActive().getEditors() //returns User[]
var user = Session.getActiveUser(); //returns User
return editors.indexOf(user) // returns -1
This happens even though the active user is an editor of the spreadsheet in question.
I can get around this issue by removing the user via email address (i.e. removeEditor(email)), but this seems like an extra step.
Does anyone know why these two User objects wouldn't be the same? Is this a bug?

Remembering search parameters

I am trying to make a small website with search filters (textbox and checkboxes). I have searched around and seen that the way to remember the past search filters ONLY AFTER refresh is using session storage.
However, the problem is that, even when the user browses around the website and comes back to the search page, he/she would still see the past search filters. I want to reset the search filters when the user is coming from a different page.
I'm thinking and searching around as to how to remove these variables stored in the session storage after the user leaves the page and only keeping it when the user refreshes.
My current code goes like this :
var selectedCompanies = new Array;
var selectedCategories = new Array;
var searchWord = "";
if(sessionStorage.getItem('selectedCompanies') != null){
selectedCompanies = JSON.parse(sessionStorage.getItem('selectedCompanies'));
alert("NOT NULL!");
//loop to recheck checkboxes
}
if(sessionStorage.getItem('selectedCategories') != null){
selectedCategories = JSON.parse(sessionStorage.getItem('selectedCategories'));
//loop to recheck checkboxes
}
if(sessionStorage.getItem('searchWord') != null){
searchWord = sessionStorage.getItem('searchWord');
$('#filter-searchbar').val(searchWord);
}
Please help as to how can I delete the variables inside the session storage after the user leaves the search page. Thanks!
Add the following code in a common JavaScript file. Reference this file on all pages except the search page:
sessionStorage.removeItem('selectedCompanies');
sessionStorage.removeItem('selectedCategories');
sessionStorage.removeItem('searchWord');
This will clear the search parameters on all other pages but allow the data to persist on the search page.

Categories