JQuery - run once per session

I have a function that is launched by the button "Calculate"

I need this line to be executed only once per session (the session can be 1 day or until the browser restarts).

$('.popup-with-form').magnificPopup('open'); 

A popup will open. When this function is executed (the pop-up window opens), if the "calculate" button is pressed again, I do not want the pop-up window to open again.

JS / JQuery Code:

 function StateChanged() { if (XmlHttp.readyState == 4 || XmlHttp.readyState == "complete") { $('.popup-with-form').magnificPopup('open'); document.getElementById("CalcSum").innerHTML = XmlHttp.responseText; document.getElementById("CalcSumPopup").innerHTML = XmlHttp.responseText; } } 

PS I know that many of these questions come up, and I tried different ways of doing something, but since I am “code-sensitive” and don’t know JQuery or JS, I can’t figure it out. I know that in jQuery there is a .one "thing, but I don’t understand how to make it work.

I really welcome any help. Thanks!

UPDATE Question answered by @MarcoBonelli - Thank you!

Working solution:

 if (!sessionStorage.alreadyClicked) { $('.popup-with-form').magnificPopup('open'); sessionStorage.alreadyClicked = "true"; } 
+3
source share
3 answers

If you want to run this line only once in a browser session, you can use sessionStorage . When you set a variable in sessionStorage , it retains its value until the browser closes (for example, until Google Chrome closes).

So you can do something like:

 if (!sessionStorage.alreadyClicked) { $('.popup-with-form').magnificPopup('open'); sessionStorage.alreadyClicked = 1; } 

Be careful with sessionStorage because it can only store string values.

If you want the line to be executed only once per page session (which means that after updating each page), you can use any variable and set it to true to remember that you already executed the line:

 if (!window.alreadyClicked) { $('.popup-with-form').magnificPopup('open'); alreadyClicked = true; } 
+12
source

Try

Edit, v2

I read about one thing, but I could not understand: (... I really need it to start only once when the CALCULATE button is pressed. - Roofing Calculator

HTML

 <!-- removed `action="javascript:GetInfo();" , accept-charset="UNKNOWN" , enctype="application/x-www-form-urlencoded" , method="post"` from `form` attributes --> <form id="formcalc" style="text-align: left;"> <!-- changed `input` `type` to `button` --> <input name="calculate" type="button" value="Calculate" /> </form> 

Js

 $("#formcalc > input[name='calculate']") .one("click", function(e) { e.stopPropagation(); GetInfo(); }); 

v1

 $("a.popup-with-form").one("click", function(e) { // do stuff, once // ieg, // `XmlHttp.onreadystatechange = StateChanged;` at `ShowSum()` $(e.target).remove(); // remove element when `click`ed once }); 

jsfiddle http://jsfiddle.net/guest271314/7K3tn/

See http://api.jquery.com/one/

+1
source

Please use below.

disableOn

zero

If the window width is less than the number in this option, the lightbox will not be opened, and the default behavior of the element will be triggered. Set to 0 to disable the behavior. This option only works when initializing Magnific Popup from the DOM element.

It can also take a function as a parameter, which should return true if the lightbox can be opened otherwise. For instance:

disableOn: function () {if ($ (condition) {return false;} return true;}

0
source

Source: https://habr.com/ru/post/1235951/


All Articles