6

I have a function that is triggered by "Calculate" button

I need this line to only run once per session (session could be 1 day or until browser is reloaded).

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

This opens a Magnific Popup. Once this function is executed (popup opens), if "calculate" button is pressed again, I don't want popup 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 many of these questions pop up, and I tried different ways of doing thing, but since I'm "code-challanged" and do not know JQuery or JS I can't figure it out. I know there is a .one "thing" in JQuery, but don't understand how to make it work.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Levchik
  • 496
  • 1
  • 5
  • 19
  • @derdida because I need calculator to still work. If I disable the button, it will stop working. – Levchik Aug 01 '14 at 00:09
  • So basically you have a self defined "session" thing, which isn't really a session, so you can't just set a flag in `sessionStorage` or a simple session cookie. If you really mean just a regular session, Google session cookies – adeneo Aug 01 '14 at 00:12

3 Answers3

20

If you want to execute this line only once per browser session you can use sessionStorage. When you set a variable on sessionStorage it keeps its value until the browser closes (e.g. until you close Google Chrome).

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 once every page refresh) then you can use any variable and set it to true to remember you already executed the line:

if (!window.alreadyClicked) {
    $('.popup-with-form').magnificPopup('open');
    alreadyClicked = true;
}
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 2
    what is `string` value? why should i be careful? (sorry - I know this is dumb question ... but I'm a roofer trying to do code :) – Levchik Aug 01 '14 at 00:19
  • @RoofingCalculator I mean that sessionStorage can only store variables of string type. If you type `sessionStorage.foo = 3` then the value will be converted into a string and then saved. So if you call `sessionStorage.foo` again it will output `"3"` (string), not `3` (number). – Marco Bonelli Aug 01 '14 at 00:22
  • Awesom, works great! Please explain what this `string` value is and why i should be careful. How is first way different from second way (besides that if paged is reloaded in second way, it will work again) – Levchik Aug 01 '14 at 00:22
  • 2
    @RoofingCalculator In the first way you store the variable in sessionStorage, so once the line is executed it will store it, and, given that sessionStorage resets only if you **close** the browser, the line will not be executed until you close and reopen the browser. In the second way you save the variable on the window object, which lives only during the page session, so if you close the page (but not the browser) and reopen it later, the variable will no longer be there and the line of code will be executed. – Marco Bonelli Aug 01 '14 at 00:28
  • @MarcoBonelli Check for `sessionStorage` in `window` first ? `undefined` at some browsers ? i.e.g., `sessionStorage = !!window.sessionStorage ? window.sessionStorage : {};` , or `if (window.sessionStorage != undefined) { // do stuff } else { window.sessionStorgae = {}; // do stuff with sessionStorage object }` ? – guest271314 Aug 01 '14 at 00:43
  • sessionStorage is **always** defined, but sometimes is unaccessible due to browser restrictions. – Marco Bonelli Aug 01 '14 at 00:44
  • Not appear to be defined at chromium 33 ? At `console`, `sessionStorage // ReferenceError: sessionStorage is not defined` , `window.sessionStorage // undefined` – guest271314 Aug 01 '14 at 00:48
  • @MarcoBonelli - I did the variant `sessionStorage` and closed the TAB (not the whole browser) and when ran this in NEW tab, it worked again (1 time) ... reloading the tab - popup did not open - so I guess reloading preserves the session. Great help - thank you very much!!! – Levchik Aug 01 '14 at 02:52
1

Try

Edit, v2

I read about .one but could not figure it out :( ... I actually need it to run once ONLY when 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
  // i.e.g., 
  // `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/

guest271314
  • 1
  • 15
  • 104
  • 177
  • I read about `.one` but could not figure it out :( ... I actually need it to run once ONLY when CALCULATE button is pressed. – Levchik Aug 01 '14 at 00:29
  • Piece at post should achieve requirement . Comment portions not _have_ to "do stuff" ; as `click` of `a` element () calls `.magnificPopup()` . `a` element then removed from `DOM`, not able to be clicked again . For clarity , when `calculate` button is clicked once, remove attached functions , not `Open POPUP` ? , or both ? Thanks – guest271314 Aug 01 '14 at 00:33
  • `Open Popup` is a link the activates popup (for testing or other purposes) ... what I needed was: When click `Calculate`, open popup only 1 time. If click `Calculate` 2nd time, popup should NOT open. The question was answered by Marco Bonelli - it's exactly what I needed. – Levchik Aug 01 '14 at 02:48
0

Please use below.

disableOn

null

If window width is less then number in this option - lightbox will not be opened and default behavior of element will be triggered. Set to 0 to disable behavior. Option works only when you initialize Magnific Popup from DOM element.

Can also accept Function as a parameter, which should return true if lightbox can be opened andfalse otherwise. For example:

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

Janty
  • 1,708
  • 2
  • 15
  • 29
  • I don't understand this at all. I need the popup to work. I just need it to open ONLY one time if "Calculate" button is pressed. There will be another (regular) way to activate popup. The way I understand it - it's much easier to do this with JQuery, than setting window width ... – Levchik Aug 01 '14 at 00:16
  • Then you can use cookies for the same http://stackoverflow.com/questions/16354301/show-welcome-div-only-once-per-user-browser-session – Janty Aug 01 '14 at 00:27