How to run javascript only once after every login?

I am trying to achieve that after user logs in to my app, I will have to send get request to my servlet, which will respond with servletOutputStream with some sound message like ('Hello User, how are you'). But I want it to be only once, not after the logged user refreshes the page and hears it again.

-------------Problems Reply------------

You can use localStorage as a general solution.

Check if the user already received the message before outputting the message:

if (!localStorage.getItem('messageSent')) {
// 1. create the message for the user here

// 2. make sure user does not get message again
localStorage.setItem('messageSent', 'true'); // any truthy value

Clear the localStorage when user logs out:


Based off your question, it seems you can even avoid the above solution and just output the message in the login logic (after user gets authenticated and logs in). When the user refreshes the page, it shouldn't show the message because the login logic should not run when the user is already logged in.

Set a cookie after the first run, and check for the presence of the cookie before. The cookie should only last the length of the current session, assuming you want the message to appear the next time they log in. A quick search should reveal how to set cookies in JS.

Category:javascript Views:0 Time:2018-03-12
Tags: javascript

Related post

  • JQuery Mobile how to run script only once on pageinit 2011-12-16

    I've got a problem with jquery mobile and nested-lists. When I load the page for the first time it looks great, but when I navigate in the jquery mobile list it dosn't run the script because it's in the document.ready() I found a solution using $('[d

  • Heroku runs query only once after deploy 2012-01-05

    I'm writing a Rails app & at the moment I'm making sitemap generator. Everything was just fine, and is still fine on my computer. But when I deploy it to Heroku I get a problem. The generator works pretty simple: it just writes all urls of one re

  • Zend Framework: How to generate javascript only once? 2010-09-28

    I am creating a custom zend form element that will require some javascript. I can add the javascript I need like this: /** * Renders the javascript to the view. */ protected function _generateJavaScript() { $this->view->headScript()->capture

  • Why Javascript only works after opening developer tools in IE once? 2011-10-12

    IE9 Bug - Javascript only works after opening developer tools once. Our site offers free pdf downloads to users, and it has a simple "enter password to download" function. However, it doesn't work at all in Internet Explorer. You can see for yourself

  • How to run a jQuery Code after loading all the images in my page? 2010-09-08

    How to run a jQuery Code after loading all the images in my page ? --------------Solutions------------- $(window).load(function(){}) Checking to see that all images have loaded is slightly involved, so unless you have a pressing need to be so precise

  • I want to use a method which is called only once after deploying to Server 2010-03-02

    I am new to Servlets. I want to use a method which is called only once after deploying to server. I looked at HttpServlet#init(). But I figured out it is called with each request. Did I misunderstand it? What are the alternatives to init()? ---------

  • When a page loads an image, does it load it only once, or every time it is found in the markup? 2010-07-27

    When a page loads an image, does it load it only once, or every time it is found in the markup? and what about jquery, does appending an img cause it to reload again? I ask this because I have a high res image, but need to you use it in many instance

  • How to run script only when user land on page 2010-12-29

    I have some script, written in Javascript, that show progress of loading site elements. I put this script on index.html file, so it runs only on main page. It works perfectly fine, but now it show progress when you land on index.html and also when yo

  • Want to display a dialog only once after the app been installed 2011-05-04

    How can I make my dialog appear only once after my Android app is installed on the device? --------------Solutions------------- Use SharedPreference to store the firstrun value, and check in your launching activity against that value. If the value is

  • How to run Stylecop only in check in? 2012-01-17

    I've to use Stylecop with VS2010 (using Team Developer) in my company, but it makes me waste a lot of time, and it's really anoying. Does anybody knows how to run Stylecop only before checking in the code, instead of in every time I build the project

  • run a php automatically after every hour 2012-02-12

    I am using a shared windows hosting in which sending 120 mails/hour is allowed though php code. i have a php page to send emails more than 200 mails at a time.But i want to run the page, after every hour(scheduled task). I will split the emails(in 10

  • Windows Live Mail - how do I open only once to read my mail? I currently have to open and close the program and then reopen to access mail. 2014-11-04

    I have to open Windows Live Mail twice to read my Email. How do I open only once to read my mail? --------------Solutions------------- Hi Mariem22, I'd like to try and help you out. Have you tried doing a repair on the email program? If not, plese fo

  • How to run JavaScript using jsc on OS X? 2011-07-26

    I got this when I was trying to find some way to run my JavaScript programs through terminal. The run and load command mentioned can execute external JavaScript files. Help me how to do this. I am trying to run JavaScript programs which are store loc

  • How to run javascript scripts directly from netbeans (using rhino) ? 2012-02-13

    I am using netbeans 7.1, i have read this manual but i don't understand how to run javascript files in netbeans, i don't find any "Run" menu entry. What is the right way to run a javascript script from ne

  • After every login I get " You must restart your computer to apply these changes" 2013-01-17

    After every login I get "You must restart your computer to apply these changes" When I click yes to restart Windows 7 restarts then I login and get the same again?? --------------Solutions------------- Hi, · Were you trying to install any windows upd

  • jQuery - How to sequentially run an animation * only once * after animation on a group (e.g. siblings) 2009-11-11

    Here's my dilema. I have 2 animations that need to run sequentially. The first animation runs on a group of elements acquired through jQuery's siblings() function. The second animation runs on a single element. (The one upon which siblings() was call

  • run postback only once with javascript 2010-04-15

    I have to do a postback automatically when a user visit a page. How can I run this postback only once? javascript:__doPostBack('dnn$ctr424$ViewLogin$btChange','') --------------Solutions------------- If form_load register that as a startup script wit

  • How to set keyboard key pressed event only once after pressed but not continously 2016-05-12

    I am using below code to move the player up, but the problem is as long as the button is pressed and held the player continues to move. How can I change this behaviour so that no matter how much time the button is pressed for the player moves only on

  • How to load fixtures only once in django unit tests ? 2009-06-11

    In unit tests I need to load fixtures, as below: class TestQuestionBankViews(TestCase): # Load fixtures fixtures = ['qbank'] def setUp(self): login = self.client.login(email="[email protected]",password="welcome") def test_starting_an_exam_view(self): c

  • Generally valid way to execute a javascript only once (via cookies)? 2011-02-26

    I'd like to add an onbeforeunload javascript, asking the user to bookmark the page (there's a small button in the header for that purpose). The problem is, no matter if they'd like to bookmark it, it's pointless and annoying after running once. So, w

  • How to run an ajax call after another ajax call? 2010-01-03

    Here is the problem that I am having. The user can log in but if they don't refresh the page they can not log back out. So how do I call another ajax call right after another? The log out link is being loaded by the fist ajax call, could that be the

  • Async template loading - How to load it only once? 2012-04-27

    I have a client-side JS app that needs to load a template to display each item. Let's say they're notes. The problem is in the asynchronous part. I can't make it so that the template is loaded only once. It loads every time a note is calling the rend

  • How to run JavaScript on the server 2009-06-02

    I'm writing an iPhone app that needs to interact with the Google Maps API, therefore I must user JavaScript (please correct me if I'm wrong) to access the results and the route created. Since the JavaScript support in native iPhone apps is quite poor

  • Ruby on Rails. How to show record only once? 2009-12-08

    I want to know how to show a record only exactly after it was created. So i want to show it only once. Just see. I have model called Claim. User creates new Claim (user is anonymous). I want to show this Claim only ofter user creates it and never aga

Copyright (C), All Rights Reserved.

processed in 0.146 (s). 11 q(s)