Persist variables between page loads










70















I am trying to capture the submit button press of my form and if the form is submitted, the page refreshes and I show a few hidden fields. I would like to capture whether the form has been submitted before or not and if it submitted on reload, I would like to unhide the hidden fields. I was trying to use a global variable to achieve this, however I was unable to make it work properly.



Here is what I tried:



 var clicked = false;

$(document).ready(function()

$("input[type='submit'][value='Search']").attr("onclick", "form.act.value='detailSearch'; clicked = true; return true;");

if (clicked == true)
// show hidden fields
else
// don't show hidden fields

);


Any suggestions on what is wrong with this code?










share|improve this question



















  • 1





    I don't think this can be done without some kind of storage. Your JS variables will all be whiped clean. Your whole scope will be recycled. Sorry Newbie, you wasted 50pnts.

    – Dave Alperovich
    May 5 '15 at 19:00











  • Why don't you put most of your page into a "partial" and refresh that?

    – Dave Alperovich
    May 5 '15 at 19:01











  • What is a partial? Sorry I am not aware of that.

    – Neophile
    May 7 '15 at 10:03











  • Have you considered submitting the form via ajax and showing the fields after the submission?

    – dannyshaw
    May 8 '15 at 12:24






  • 1





    Can you elaborate on why you need the page to refresh? If we can avoid it, you may able to do what you want if you just preventDefault on the submit event (instead of the click you're using.)

    – Peleg
    May 11 '15 at 21:29















70















I am trying to capture the submit button press of my form and if the form is submitted, the page refreshes and I show a few hidden fields. I would like to capture whether the form has been submitted before or not and if it submitted on reload, I would like to unhide the hidden fields. I was trying to use a global variable to achieve this, however I was unable to make it work properly.



Here is what I tried:



 var clicked = false;

$(document).ready(function()

$("input[type='submit'][value='Search']").attr("onclick", "form.act.value='detailSearch'; clicked = true; return true;");

if (clicked == true)
// show hidden fields
else
// don't show hidden fields

);


Any suggestions on what is wrong with this code?










share|improve this question



















  • 1





    I don't think this can be done without some kind of storage. Your JS variables will all be whiped clean. Your whole scope will be recycled. Sorry Newbie, you wasted 50pnts.

    – Dave Alperovich
    May 5 '15 at 19:00











  • Why don't you put most of your page into a "partial" and refresh that?

    – Dave Alperovich
    May 5 '15 at 19:01











  • What is a partial? Sorry I am not aware of that.

    – Neophile
    May 7 '15 at 10:03











  • Have you considered submitting the form via ajax and showing the fields after the submission?

    – dannyshaw
    May 8 '15 at 12:24






  • 1





    Can you elaborate on why you need the page to refresh? If we can avoid it, you may able to do what you want if you just preventDefault on the submit event (instead of the click you're using.)

    – Peleg
    May 11 '15 at 21:29













70












70








70


47






I am trying to capture the submit button press of my form and if the form is submitted, the page refreshes and I show a few hidden fields. I would like to capture whether the form has been submitted before or not and if it submitted on reload, I would like to unhide the hidden fields. I was trying to use a global variable to achieve this, however I was unable to make it work properly.



Here is what I tried:



 var clicked = false;

$(document).ready(function()

$("input[type='submit'][value='Search']").attr("onclick", "form.act.value='detailSearch'; clicked = true; return true;");

if (clicked == true)
// show hidden fields
else
// don't show hidden fields

);


Any suggestions on what is wrong with this code?










share|improve this question
















I am trying to capture the submit button press of my form and if the form is submitted, the page refreshes and I show a few hidden fields. I would like to capture whether the form has been submitted before or not and if it submitted on reload, I would like to unhide the hidden fields. I was trying to use a global variable to achieve this, however I was unable to make it work properly.



Here is what I tried:



 var clicked = false;

$(document).ready(function()

$("input[type='submit'][value='Search']").attr("onclick", "form.act.value='detailSearch'; clicked = true; return true;");

if (clicked == true)
// show hidden fields
else
// don't show hidden fields

);


Any suggestions on what is wrong with this code?







javascript jquery form-submit persistent-storage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 5 '17 at 20:13









kryger

9,40153754




9,40153754










asked May 1 '15 at 12:13









NeophileNeophile

2,217103787




2,217103787







  • 1





    I don't think this can be done without some kind of storage. Your JS variables will all be whiped clean. Your whole scope will be recycled. Sorry Newbie, you wasted 50pnts.

    – Dave Alperovich
    May 5 '15 at 19:00











  • Why don't you put most of your page into a "partial" and refresh that?

    – Dave Alperovich
    May 5 '15 at 19:01











  • What is a partial? Sorry I am not aware of that.

    – Neophile
    May 7 '15 at 10:03











  • Have you considered submitting the form via ajax and showing the fields after the submission?

    – dannyshaw
    May 8 '15 at 12:24






  • 1





    Can you elaborate on why you need the page to refresh? If we can avoid it, you may able to do what you want if you just preventDefault on the submit event (instead of the click you're using.)

    – Peleg
    May 11 '15 at 21:29












  • 1





    I don't think this can be done without some kind of storage. Your JS variables will all be whiped clean. Your whole scope will be recycled. Sorry Newbie, you wasted 50pnts.

    – Dave Alperovich
    May 5 '15 at 19:00











  • Why don't you put most of your page into a "partial" and refresh that?

    – Dave Alperovich
    May 5 '15 at 19:01











  • What is a partial? Sorry I am not aware of that.

    – Neophile
    May 7 '15 at 10:03











  • Have you considered submitting the form via ajax and showing the fields after the submission?

    – dannyshaw
    May 8 '15 at 12:24






  • 1





    Can you elaborate on why you need the page to refresh? If we can avoid it, you may able to do what you want if you just preventDefault on the submit event (instead of the click you're using.)

    – Peleg
    May 11 '15 at 21:29







1




1





I don't think this can be done without some kind of storage. Your JS variables will all be whiped clean. Your whole scope will be recycled. Sorry Newbie, you wasted 50pnts.

– Dave Alperovich
May 5 '15 at 19:00





I don't think this can be done without some kind of storage. Your JS variables will all be whiped clean. Your whole scope will be recycled. Sorry Newbie, you wasted 50pnts.

– Dave Alperovich
May 5 '15 at 19:00













Why don't you put most of your page into a "partial" and refresh that?

– Dave Alperovich
May 5 '15 at 19:01





Why don't you put most of your page into a "partial" and refresh that?

– Dave Alperovich
May 5 '15 at 19:01













What is a partial? Sorry I am not aware of that.

– Neophile
May 7 '15 at 10:03





What is a partial? Sorry I am not aware of that.

– Neophile
May 7 '15 at 10:03













Have you considered submitting the form via ajax and showing the fields after the submission?

– dannyshaw
May 8 '15 at 12:24





Have you considered submitting the form via ajax and showing the fields after the submission?

– dannyshaw
May 8 '15 at 12:24




1




1





Can you elaborate on why you need the page to refresh? If we can avoid it, you may able to do what you want if you just preventDefault on the submit event (instead of the click you're using.)

– Peleg
May 11 '15 at 21:29





Can you elaborate on why you need the page to refresh? If we can avoid it, you may able to do what you want if you just preventDefault on the submit event (instead of the click you're using.)

– Peleg
May 11 '15 at 21:29












4 Answers
4






active

oldest

votes


















153





+175









As HTTP is stateless, every time you load the page it will use the initial values of whatever you set in JavaScript. You can't set a global variable in JS and simply make that value stay after loading the page again.



There are a couple of ways you could store the value in another place so that you can initialize it on load using JavaScript




Query string



When submitting a form using the GET method, the url gets updated with a query string (?parameter=value&something=42). You can utilize this by setting an input field in the form to a certain value. This would be the simplest example:



<form method="GET">
<input type="hidden" name="clicked" value="true" />
<input type="submit" />
</form>


On initial load of the page, no query string is set. When you submit this form, the name and value combination of the input are passed in the query string as clicked=true. So when the page loads again with that query string you can check if the button was clicked.



To read this data, you can use the following script on page load:



function getParameterByName(name) 
name = name.replace(/[/, "\[").replace(/[]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/+/g, " "));


var clicked = getParameterByName('clicked');


(Source)



Ability to use this depends on how your form currently works, if you already use a POST then this could be problematic.



In addition, for larger sets of data this is less than optimal. Passing around a string isn't a big deal but for arrays and objects of data you should probably use Web Storage or cookies. While the details differ a bit across browsers, the practical limit to URI length is around 2000 characters




Web Storage



With the introduction of HTML5 we also got Web Storage, which allows you to save information in the browser across page loads. There is localStorage which can save data for a longer period (as long as the user doesn't manually clear it) and sessionStorage which saves data only during your current browsing session. The latter is useful for you here, because you don't want to keep "clicked" set to true when the user comes back later.



Here I set the storage on the button click event, but you could also bind it to form submit or anything else.



$('input[type="submit"][value="Search"]').click(function() 
sessionStorage.setItem('clicked', 'true');
);


Then when you load the page, you can check if it's set using this:



var clicked = sessionStorage.getItem('clicked');


Even though this value is only saved during this browsing session, it might be possible you want to reset it earlier. To do so, use:



sessionStorage.removeItem('clicked');


If you would want to save a JS object or array you should convert that to a string. According to the spec it should be possible to save other datatypes, but this isn't correctly implemented across browsers yet.



//set
localStorage.setItem('myObject', JSON.stringify(myObject));

//get
var myObject = JSON.parse(localStorage.getItem('myObject'));


Browser support is pretty great so you should be safe to use this unless you need to support really old/obscure browsers. Web Storage is the future.




Cookies



An alternative to Web Storage is saving the data in a cookie. Cookies are mainly made to read data server-side, but can be used for purely client-side data as well.



You already use jQuery, which makes setting cookies quite easy. Again, I use the click event here but could be used anywhere.



$('input[type="submit"][value="Search"]').click(function() 
$.cookie('clicked', 'true', expires: 1); // expires in 1 day
);


Then on page load you can read the cookie like this:



var clicked = $.cookie('clicked');


As cookies persist across sessions in your case you will need to unset them as soon as you've done whatever you need to do with it. You wouldn't want the user to come back a day later and still have clicked set to true.



if(clicked === "true") 
//doYourStuff();
$.cookie('clicked', null);



(a non-jQuery way to set/read cookies can be found right here)



I personally wouldn't use a cookie for something simple as remembering a clicked state, but if the query string isn't an option and you need to support really old browsers that don't support sessionStorage this will work. You should implement that with a check for sessionStorage first, and only if that fails use the cookie method.




window.name



Although this seems like a hack to me that probably originated from before localStorage/sessionStorage, you could store information in the window.name property:



window.name = "my value"


It can only store strings, so if you want to save an object you'll have to stringify it just like the above localStorage example:



window.name = JSON.stringify( clicked: true );


The major difference is that this information is retained across not only page refreshes but also different domains. However, it is restricted to the current tab you're in.



This means you could save some information on your page and as long as the user stays in that tab, you could access that same information even if he browsed to another website and back. In general, I would advice against using this unless you need to actually store cross-domain information during a single browsing session.






share|improve this answer




















  • 5





    Ugh, only after writing did I read your bounty description that said "no cookies or local storage". I'll keep this comment intact for others that might need it, but you'll just need to use the query string I guess.

    – Stephan Muller
    May 6 '15 at 8:37






  • 1





    Basically I am already using localStorage in a lot of cases for my web application and I would like to avoid using too many localStorage variables. However I do like the idea of removing the localStorage item once I am done with it. That was something I did not think of when using my localStorage variable.

    – Neophile
    May 7 '15 at 10:43






  • 3





    In case of post request you can modify the action -attribute $("#formid").attr("action", url+"&clicked=1") and also detect GET or POST from $("#formid").attr("method") but it may not solve how to detect the reload problem

    – Tero Tolonen
    May 11 '15 at 22:34






  • 1





    I wanted to add that local and session storage are not totally reliable on newer browsers aswell. Usually there is a fallback to window.name.

    – JavaScript
    Jul 20 '15 at 11:34






  • 1





    Great answer but this statement "According to the spec it should be possible to save other datatypes" (about web storage) ain't correct AFAICS. The storage interface is specified to be dealing with DOMStrings rather than arbitrary data types. For non-DOMString types it may be helpful to use super-json to stringify more complex types.

    – Marcel Stör
    Mar 12 '16 at 10:24



















5














Try utilizing $.holdReady() , history



<script src="jquery.js" type="text/javascript"></script>
</head>
<body>
<form method="POST">
<input type="text" name="name" value="" />
<input type="submit" value="Search" />
<input type="hidden" />
<input type="hidden" />
</form>
<script type="text/javascript">
function show()
return $("form input[type=hidden]")
.replaceWith(function(i, el)
return "<input type=text>"
);


$.holdReady(true);
if (history.state !== null && history.state.clicked === true)
// show hidden fields
// if `history.state.clicked === true` ,
// replace `input type=hidden` with `input type=text`
show();
console.log(history);

else
// don't show hidden fields
console.log(history);

$.holdReady(false);

$(document).ready(function()

$("input[type=submit][value=Search]")
.on("click", function(e)
e.preventDefault();
if (history.state === null)
// do stuff
history.pushState("clicked":true);
// replace `input type=hidden` with `input type=text`
show();
console.log(history);
else
// do other stuff
;
);

);
</script>
</body>





share|improve this answer























  • I will give that a go. Thanks for your answer.

    – Neophile
    May 11 '15 at 16:25


















1














Using localeStorage or sessionStorage seems to be the best bet.



Intead of saving the clicked variable in the globle scope store it this way:



if(localeStorage.getItem("clicked") === null)
localeStorage.setItem("clicked", "FALSE"); // for the first time

$(document).ready(function()

$("input[type='submit'][value='Search']").attr("onclick", "form.act.value='detailSearch';return true;");

var clicked = localeStorage.getItem("clicked") == "FALSE" ? "TRUE" : "FALSE";

localeStorage.setItem("clicked", clicked);

if (clicked == "TRUE")
// show hidden fields
else
// don't show hidden fields


);





share|improve this answer























  • I am intending on not using localstorage or sessionStorage preferably.

    – Neophile
    May 7 '15 at 9:33


















-3














You could try this:



 $("input[type='submit'][value='Search']").click(function()
form.act.value='detailSearch';
clicked = true;
return true;
);





share|improve this answer

























  • When the page reloads, the still get the clicked value as false.

    – Neophile
    May 1 '15 at 12:16






  • 1





    @TheNewbie do you think you can pass a query string? with page refresh the current approach will not work.

    – renakre
    May 1 '15 at 12:19











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29986657%2fpersist-variables-between-page-loads%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









153





+175









As HTTP is stateless, every time you load the page it will use the initial values of whatever you set in JavaScript. You can't set a global variable in JS and simply make that value stay after loading the page again.



There are a couple of ways you could store the value in another place so that you can initialize it on load using JavaScript




Query string



When submitting a form using the GET method, the url gets updated with a query string (?parameter=value&something=42). You can utilize this by setting an input field in the form to a certain value. This would be the simplest example:



<form method="GET">
<input type="hidden" name="clicked" value="true" />
<input type="submit" />
</form>


On initial load of the page, no query string is set. When you submit this form, the name and value combination of the input are passed in the query string as clicked=true. So when the page loads again with that query string you can check if the button was clicked.



To read this data, you can use the following script on page load:



function getParameterByName(name) 
name = name.replace(/[/, "\[").replace(/[]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/+/g, " "));


var clicked = getParameterByName('clicked');


(Source)



Ability to use this depends on how your form currently works, if you already use a POST then this could be problematic.



In addition, for larger sets of data this is less than optimal. Passing around a string isn't a big deal but for arrays and objects of data you should probably use Web Storage or cookies. While the details differ a bit across browsers, the practical limit to URI length is around 2000 characters




Web Storage



With the introduction of HTML5 we also got Web Storage, which allows you to save information in the browser across page loads. There is localStorage which can save data for a longer period (as long as the user doesn't manually clear it) and sessionStorage which saves data only during your current browsing session. The latter is useful for you here, because you don't want to keep "clicked" set to true when the user comes back later.



Here I set the storage on the button click event, but you could also bind it to form submit or anything else.



$('input[type="submit"][value="Search"]').click(function() 
sessionStorage.setItem('clicked', 'true');
);


Then when you load the page, you can check if it's set using this:



var clicked = sessionStorage.getItem('clicked');


Even though this value is only saved during this browsing session, it might be possible you want to reset it earlier. To do so, use:



sessionStorage.removeItem('clicked');


If you would want to save a JS object or array you should convert that to a string. According to the spec it should be possible to save other datatypes, but this isn't correctly implemented across browsers yet.



//set
localStorage.setItem('myObject', JSON.stringify(myObject));

//get
var myObject = JSON.parse(localStorage.getItem('myObject'));


Browser support is pretty great so you should be safe to use this unless you need to support really old/obscure browsers. Web Storage is the future.




Cookies



An alternative to Web Storage is saving the data in a cookie. Cookies are mainly made to read data server-side, but can be used for purely client-side data as well.



You already use jQuery, which makes setting cookies quite easy. Again, I use the click event here but could be used anywhere.



$('input[type="submit"][value="Search"]').click(function() 
$.cookie('clicked', 'true', expires: 1); // expires in 1 day
);


Then on page load you can read the cookie like this:



var clicked = $.cookie('clicked');


As cookies persist across sessions in your case you will need to unset them as soon as you've done whatever you need to do with it. You wouldn't want the user to come back a day later and still have clicked set to true.



if(clicked === "true") 
//doYourStuff();
$.cookie('clicked', null);



(a non-jQuery way to set/read cookies can be found right here)



I personally wouldn't use a cookie for something simple as remembering a clicked state, but if the query string isn't an option and you need to support really old browsers that don't support sessionStorage this will work. You should implement that with a check for sessionStorage first, and only if that fails use the cookie method.




window.name



Although this seems like a hack to me that probably originated from before localStorage/sessionStorage, you could store information in the window.name property:



window.name = "my value"


It can only store strings, so if you want to save an object you'll have to stringify it just like the above localStorage example:



window.name = JSON.stringify( clicked: true );


The major difference is that this information is retained across not only page refreshes but also different domains. However, it is restricted to the current tab you're in.



This means you could save some information on your page and as long as the user stays in that tab, you could access that same information even if he browsed to another website and back. In general, I would advice against using this unless you need to actually store cross-domain information during a single browsing session.






share|improve this answer




















  • 5





    Ugh, only after writing did I read your bounty description that said "no cookies or local storage". I'll keep this comment intact for others that might need it, but you'll just need to use the query string I guess.

    – Stephan Muller
    May 6 '15 at 8:37






  • 1





    Basically I am already using localStorage in a lot of cases for my web application and I would like to avoid using too many localStorage variables. However I do like the idea of removing the localStorage item once I am done with it. That was something I did not think of when using my localStorage variable.

    – Neophile
    May 7 '15 at 10:43






  • 3





    In case of post request you can modify the action -attribute $("#formid").attr("action", url+"&clicked=1") and also detect GET or POST from $("#formid").attr("method") but it may not solve how to detect the reload problem

    – Tero Tolonen
    May 11 '15 at 22:34






  • 1





    I wanted to add that local and session storage are not totally reliable on newer browsers aswell. Usually there is a fallback to window.name.

    – JavaScript
    Jul 20 '15 at 11:34






  • 1





    Great answer but this statement "According to the spec it should be possible to save other datatypes" (about web storage) ain't correct AFAICS. The storage interface is specified to be dealing with DOMStrings rather than arbitrary data types. For non-DOMString types it may be helpful to use super-json to stringify more complex types.

    – Marcel Stör
    Mar 12 '16 at 10:24
















153





+175









As HTTP is stateless, every time you load the page it will use the initial values of whatever you set in JavaScript. You can't set a global variable in JS and simply make that value stay after loading the page again.



There are a couple of ways you could store the value in another place so that you can initialize it on load using JavaScript




Query string



When submitting a form using the GET method, the url gets updated with a query string (?parameter=value&something=42). You can utilize this by setting an input field in the form to a certain value. This would be the simplest example:



<form method="GET">
<input type="hidden" name="clicked" value="true" />
<input type="submit" />
</form>


On initial load of the page, no query string is set. When you submit this form, the name and value combination of the input are passed in the query string as clicked=true. So when the page loads again with that query string you can check if the button was clicked.



To read this data, you can use the following script on page load:



function getParameterByName(name) 
name = name.replace(/[/, "\[").replace(/[]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/+/g, " "));


var clicked = getParameterByName('clicked');


(Source)



Ability to use this depends on how your form currently works, if you already use a POST then this could be problematic.



In addition, for larger sets of data this is less than optimal. Passing around a string isn't a big deal but for arrays and objects of data you should probably use Web Storage or cookies. While the details differ a bit across browsers, the practical limit to URI length is around 2000 characters




Web Storage



With the introduction of HTML5 we also got Web Storage, which allows you to save information in the browser across page loads. There is localStorage which can save data for a longer period (as long as the user doesn't manually clear it) and sessionStorage which saves data only during your current browsing session. The latter is useful for you here, because you don't want to keep "clicked" set to true when the user comes back later.



Here I set the storage on the button click event, but you could also bind it to form submit or anything else.



$('input[type="submit"][value="Search"]').click(function() 
sessionStorage.setItem('clicked', 'true');
);


Then when you load the page, you can check if it's set using this:



var clicked = sessionStorage.getItem('clicked');


Even though this value is only saved during this browsing session, it might be possible you want to reset it earlier. To do so, use:



sessionStorage.removeItem('clicked');


If you would want to save a JS object or array you should convert that to a string. According to the spec it should be possible to save other datatypes, but this isn't correctly implemented across browsers yet.



//set
localStorage.setItem('myObject', JSON.stringify(myObject));

//get
var myObject = JSON.parse(localStorage.getItem('myObject'));


Browser support is pretty great so you should be safe to use this unless you need to support really old/obscure browsers. Web Storage is the future.




Cookies



An alternative to Web Storage is saving the data in a cookie. Cookies are mainly made to read data server-side, but can be used for purely client-side data as well.



You already use jQuery, which makes setting cookies quite easy. Again, I use the click event here but could be used anywhere.



$('input[type="submit"][value="Search"]').click(function() 
$.cookie('clicked', 'true', expires: 1); // expires in 1 day
);


Then on page load you can read the cookie like this:



var clicked = $.cookie('clicked');


As cookies persist across sessions in your case you will need to unset them as soon as you've done whatever you need to do with it. You wouldn't want the user to come back a day later and still have clicked set to true.



if(clicked === "true") 
//doYourStuff();
$.cookie('clicked', null);



(a non-jQuery way to set/read cookies can be found right here)



I personally wouldn't use a cookie for something simple as remembering a clicked state, but if the query string isn't an option and you need to support really old browsers that don't support sessionStorage this will work. You should implement that with a check for sessionStorage first, and only if that fails use the cookie method.




window.name



Although this seems like a hack to me that probably originated from before localStorage/sessionStorage, you could store information in the window.name property:



window.name = "my value"


It can only store strings, so if you want to save an object you'll have to stringify it just like the above localStorage example:



window.name = JSON.stringify( clicked: true );


The major difference is that this information is retained across not only page refreshes but also different domains. However, it is restricted to the current tab you're in.



This means you could save some information on your page and as long as the user stays in that tab, you could access that same information even if he browsed to another website and back. In general, I would advice against using this unless you need to actually store cross-domain information during a single browsing session.






share|improve this answer




















  • 5





    Ugh, only after writing did I read your bounty description that said "no cookies or local storage". I'll keep this comment intact for others that might need it, but you'll just need to use the query string I guess.

    – Stephan Muller
    May 6 '15 at 8:37






  • 1





    Basically I am already using localStorage in a lot of cases for my web application and I would like to avoid using too many localStorage variables. However I do like the idea of removing the localStorage item once I am done with it. That was something I did not think of when using my localStorage variable.

    – Neophile
    May 7 '15 at 10:43






  • 3





    In case of post request you can modify the action -attribute $("#formid").attr("action", url+"&clicked=1") and also detect GET or POST from $("#formid").attr("method") but it may not solve how to detect the reload problem

    – Tero Tolonen
    May 11 '15 at 22:34






  • 1





    I wanted to add that local and session storage are not totally reliable on newer browsers aswell. Usually there is a fallback to window.name.

    – JavaScript
    Jul 20 '15 at 11:34






  • 1





    Great answer but this statement "According to the spec it should be possible to save other datatypes" (about web storage) ain't correct AFAICS. The storage interface is specified to be dealing with DOMStrings rather than arbitrary data types. For non-DOMString types it may be helpful to use super-json to stringify more complex types.

    – Marcel Stör
    Mar 12 '16 at 10:24














153





+175







153





+175



153




+175





As HTTP is stateless, every time you load the page it will use the initial values of whatever you set in JavaScript. You can't set a global variable in JS and simply make that value stay after loading the page again.



There are a couple of ways you could store the value in another place so that you can initialize it on load using JavaScript




Query string



When submitting a form using the GET method, the url gets updated with a query string (?parameter=value&something=42). You can utilize this by setting an input field in the form to a certain value. This would be the simplest example:



<form method="GET">
<input type="hidden" name="clicked" value="true" />
<input type="submit" />
</form>


On initial load of the page, no query string is set. When you submit this form, the name and value combination of the input are passed in the query string as clicked=true. So when the page loads again with that query string you can check if the button was clicked.



To read this data, you can use the following script on page load:



function getParameterByName(name) 
name = name.replace(/[/, "\[").replace(/[]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/+/g, " "));


var clicked = getParameterByName('clicked');


(Source)



Ability to use this depends on how your form currently works, if you already use a POST then this could be problematic.



In addition, for larger sets of data this is less than optimal. Passing around a string isn't a big deal but for arrays and objects of data you should probably use Web Storage or cookies. While the details differ a bit across browsers, the practical limit to URI length is around 2000 characters




Web Storage



With the introduction of HTML5 we also got Web Storage, which allows you to save information in the browser across page loads. There is localStorage which can save data for a longer period (as long as the user doesn't manually clear it) and sessionStorage which saves data only during your current browsing session. The latter is useful for you here, because you don't want to keep "clicked" set to true when the user comes back later.



Here I set the storage on the button click event, but you could also bind it to form submit or anything else.



$('input[type="submit"][value="Search"]').click(function() 
sessionStorage.setItem('clicked', 'true');
);


Then when you load the page, you can check if it's set using this:



var clicked = sessionStorage.getItem('clicked');


Even though this value is only saved during this browsing session, it might be possible you want to reset it earlier. To do so, use:



sessionStorage.removeItem('clicked');


If you would want to save a JS object or array you should convert that to a string. According to the spec it should be possible to save other datatypes, but this isn't correctly implemented across browsers yet.



//set
localStorage.setItem('myObject', JSON.stringify(myObject));

//get
var myObject = JSON.parse(localStorage.getItem('myObject'));


Browser support is pretty great so you should be safe to use this unless you need to support really old/obscure browsers. Web Storage is the future.




Cookies



An alternative to Web Storage is saving the data in a cookie. Cookies are mainly made to read data server-side, but can be used for purely client-side data as well.



You already use jQuery, which makes setting cookies quite easy. Again, I use the click event here but could be used anywhere.



$('input[type="submit"][value="Search"]').click(function() 
$.cookie('clicked', 'true', expires: 1); // expires in 1 day
);


Then on page load you can read the cookie like this:



var clicked = $.cookie('clicked');


As cookies persist across sessions in your case you will need to unset them as soon as you've done whatever you need to do with it. You wouldn't want the user to come back a day later and still have clicked set to true.



if(clicked === "true") 
//doYourStuff();
$.cookie('clicked', null);



(a non-jQuery way to set/read cookies can be found right here)



I personally wouldn't use a cookie for something simple as remembering a clicked state, but if the query string isn't an option and you need to support really old browsers that don't support sessionStorage this will work. You should implement that with a check for sessionStorage first, and only if that fails use the cookie method.




window.name



Although this seems like a hack to me that probably originated from before localStorage/sessionStorage, you could store information in the window.name property:



window.name = "my value"


It can only store strings, so if you want to save an object you'll have to stringify it just like the above localStorage example:



window.name = JSON.stringify( clicked: true );


The major difference is that this information is retained across not only page refreshes but also different domains. However, it is restricted to the current tab you're in.



This means you could save some information on your page and as long as the user stays in that tab, you could access that same information even if he browsed to another website and back. In general, I would advice against using this unless you need to actually store cross-domain information during a single browsing session.






share|improve this answer















As HTTP is stateless, every time you load the page it will use the initial values of whatever you set in JavaScript. You can't set a global variable in JS and simply make that value stay after loading the page again.



There are a couple of ways you could store the value in another place so that you can initialize it on load using JavaScript




Query string



When submitting a form using the GET method, the url gets updated with a query string (?parameter=value&something=42). You can utilize this by setting an input field in the form to a certain value. This would be the simplest example:



<form method="GET">
<input type="hidden" name="clicked" value="true" />
<input type="submit" />
</form>


On initial load of the page, no query string is set. When you submit this form, the name and value combination of the input are passed in the query string as clicked=true. So when the page loads again with that query string you can check if the button was clicked.



To read this data, you can use the following script on page load:



function getParameterByName(name) 
name = name.replace(/[/, "\[").replace(/[]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/+/g, " "));


var clicked = getParameterByName('clicked');


(Source)



Ability to use this depends on how your form currently works, if you already use a POST then this could be problematic.



In addition, for larger sets of data this is less than optimal. Passing around a string isn't a big deal but for arrays and objects of data you should probably use Web Storage or cookies. While the details differ a bit across browsers, the practical limit to URI length is around 2000 characters




Web Storage



With the introduction of HTML5 we also got Web Storage, which allows you to save information in the browser across page loads. There is localStorage which can save data for a longer period (as long as the user doesn't manually clear it) and sessionStorage which saves data only during your current browsing session. The latter is useful for you here, because you don't want to keep "clicked" set to true when the user comes back later.



Here I set the storage on the button click event, but you could also bind it to form submit or anything else.



$('input[type="submit"][value="Search"]').click(function() 
sessionStorage.setItem('clicked', 'true');
);


Then when you load the page, you can check if it's set using this:



var clicked = sessionStorage.getItem('clicked');


Even though this value is only saved during this browsing session, it might be possible you want to reset it earlier. To do so, use:



sessionStorage.removeItem('clicked');


If you would want to save a JS object or array you should convert that to a string. According to the spec it should be possible to save other datatypes, but this isn't correctly implemented across browsers yet.



//set
localStorage.setItem('myObject', JSON.stringify(myObject));

//get
var myObject = JSON.parse(localStorage.getItem('myObject'));


Browser support is pretty great so you should be safe to use this unless you need to support really old/obscure browsers. Web Storage is the future.




Cookies



An alternative to Web Storage is saving the data in a cookie. Cookies are mainly made to read data server-side, but can be used for purely client-side data as well.



You already use jQuery, which makes setting cookies quite easy. Again, I use the click event here but could be used anywhere.



$('input[type="submit"][value="Search"]').click(function() 
$.cookie('clicked', 'true', expires: 1); // expires in 1 day
);


Then on page load you can read the cookie like this:



var clicked = $.cookie('clicked');


As cookies persist across sessions in your case you will need to unset them as soon as you've done whatever you need to do with it. You wouldn't want the user to come back a day later and still have clicked set to true.



if(clicked === "true") 
//doYourStuff();
$.cookie('clicked', null);



(a non-jQuery way to set/read cookies can be found right here)



I personally wouldn't use a cookie for something simple as remembering a clicked state, but if the query string isn't an option and you need to support really old browsers that don't support sessionStorage this will work. You should implement that with a check for sessionStorage first, and only if that fails use the cookie method.




window.name



Although this seems like a hack to me that probably originated from before localStorage/sessionStorage, you could store information in the window.name property:



window.name = "my value"


It can only store strings, so if you want to save an object you'll have to stringify it just like the above localStorage example:



window.name = JSON.stringify( clicked: true );


The major difference is that this information is retained across not only page refreshes but also different domains. However, it is restricted to the current tab you're in.



This means you could save some information on your page and as long as the user stays in that tab, you could access that same information even if he browsed to another website and back. In general, I would advice against using this unless you need to actually store cross-domain information during a single browsing session.







share|improve this answer














share|improve this answer



share|improve this answer








edited May 23 '17 at 11:33









Community

11




11










answered May 6 '15 at 7:29









Stephan MullerStephan Muller

20.3k1170110




20.3k1170110







  • 5





    Ugh, only after writing did I read your bounty description that said "no cookies or local storage". I'll keep this comment intact for others that might need it, but you'll just need to use the query string I guess.

    – Stephan Muller
    May 6 '15 at 8:37






  • 1





    Basically I am already using localStorage in a lot of cases for my web application and I would like to avoid using too many localStorage variables. However I do like the idea of removing the localStorage item once I am done with it. That was something I did not think of when using my localStorage variable.

    – Neophile
    May 7 '15 at 10:43






  • 3





    In case of post request you can modify the action -attribute $("#formid").attr("action", url+"&clicked=1") and also detect GET or POST from $("#formid").attr("method") but it may not solve how to detect the reload problem

    – Tero Tolonen
    May 11 '15 at 22:34






  • 1





    I wanted to add that local and session storage are not totally reliable on newer browsers aswell. Usually there is a fallback to window.name.

    – JavaScript
    Jul 20 '15 at 11:34






  • 1





    Great answer but this statement "According to the spec it should be possible to save other datatypes" (about web storage) ain't correct AFAICS. The storage interface is specified to be dealing with DOMStrings rather than arbitrary data types. For non-DOMString types it may be helpful to use super-json to stringify more complex types.

    – Marcel Stör
    Mar 12 '16 at 10:24













  • 5





    Ugh, only after writing did I read your bounty description that said "no cookies or local storage". I'll keep this comment intact for others that might need it, but you'll just need to use the query string I guess.

    – Stephan Muller
    May 6 '15 at 8:37






  • 1





    Basically I am already using localStorage in a lot of cases for my web application and I would like to avoid using too many localStorage variables. However I do like the idea of removing the localStorage item once I am done with it. That was something I did not think of when using my localStorage variable.

    – Neophile
    May 7 '15 at 10:43






  • 3





    In case of post request you can modify the action -attribute $("#formid").attr("action", url+"&clicked=1") and also detect GET or POST from $("#formid").attr("method") but it may not solve how to detect the reload problem

    – Tero Tolonen
    May 11 '15 at 22:34






  • 1





    I wanted to add that local and session storage are not totally reliable on newer browsers aswell. Usually there is a fallback to window.name.

    – JavaScript
    Jul 20 '15 at 11:34






  • 1





    Great answer but this statement "According to the spec it should be possible to save other datatypes" (about web storage) ain't correct AFAICS. The storage interface is specified to be dealing with DOMStrings rather than arbitrary data types. For non-DOMString types it may be helpful to use super-json to stringify more complex types.

    – Marcel Stör
    Mar 12 '16 at 10:24








5




5





Ugh, only after writing did I read your bounty description that said "no cookies or local storage". I'll keep this comment intact for others that might need it, but you'll just need to use the query string I guess.

– Stephan Muller
May 6 '15 at 8:37





Ugh, only after writing did I read your bounty description that said "no cookies or local storage". I'll keep this comment intact for others that might need it, but you'll just need to use the query string I guess.

– Stephan Muller
May 6 '15 at 8:37




1




1





Basically I am already using localStorage in a lot of cases for my web application and I would like to avoid using too many localStorage variables. However I do like the idea of removing the localStorage item once I am done with it. That was something I did not think of when using my localStorage variable.

– Neophile
May 7 '15 at 10:43





Basically I am already using localStorage in a lot of cases for my web application and I would like to avoid using too many localStorage variables. However I do like the idea of removing the localStorage item once I am done with it. That was something I did not think of when using my localStorage variable.

– Neophile
May 7 '15 at 10:43




3




3





In case of post request you can modify the action -attribute $("#formid").attr("action", url+"&clicked=1") and also detect GET or POST from $("#formid").attr("method") but it may not solve how to detect the reload problem

– Tero Tolonen
May 11 '15 at 22:34





In case of post request you can modify the action -attribute $("#formid").attr("action", url+"&clicked=1") and also detect GET or POST from $("#formid").attr("method") but it may not solve how to detect the reload problem

– Tero Tolonen
May 11 '15 at 22:34




1




1





I wanted to add that local and session storage are not totally reliable on newer browsers aswell. Usually there is a fallback to window.name.

– JavaScript
Jul 20 '15 at 11:34





I wanted to add that local and session storage are not totally reliable on newer browsers aswell. Usually there is a fallback to window.name.

– JavaScript
Jul 20 '15 at 11:34




1




1





Great answer but this statement "According to the spec it should be possible to save other datatypes" (about web storage) ain't correct AFAICS. The storage interface is specified to be dealing with DOMStrings rather than arbitrary data types. For non-DOMString types it may be helpful to use super-json to stringify more complex types.

– Marcel Stör
Mar 12 '16 at 10:24






Great answer but this statement "According to the spec it should be possible to save other datatypes" (about web storage) ain't correct AFAICS. The storage interface is specified to be dealing with DOMStrings rather than arbitrary data types. For non-DOMString types it may be helpful to use super-json to stringify more complex types.

– Marcel Stör
Mar 12 '16 at 10:24














5














Try utilizing $.holdReady() , history



<script src="jquery.js" type="text/javascript"></script>
</head>
<body>
<form method="POST">
<input type="text" name="name" value="" />
<input type="submit" value="Search" />
<input type="hidden" />
<input type="hidden" />
</form>
<script type="text/javascript">
function show()
return $("form input[type=hidden]")
.replaceWith(function(i, el)
return "<input type=text>"
);


$.holdReady(true);
if (history.state !== null && history.state.clicked === true)
// show hidden fields
// if `history.state.clicked === true` ,
// replace `input type=hidden` with `input type=text`
show();
console.log(history);

else
// don't show hidden fields
console.log(history);

$.holdReady(false);

$(document).ready(function()

$("input[type=submit][value=Search]")
.on("click", function(e)
e.preventDefault();
if (history.state === null)
// do stuff
history.pushState("clicked":true);
// replace `input type=hidden` with `input type=text`
show();
console.log(history);
else
// do other stuff
;
);

);
</script>
</body>





share|improve this answer























  • I will give that a go. Thanks for your answer.

    – Neophile
    May 11 '15 at 16:25















5














Try utilizing $.holdReady() , history



<script src="jquery.js" type="text/javascript"></script>
</head>
<body>
<form method="POST">
<input type="text" name="name" value="" />
<input type="submit" value="Search" />
<input type="hidden" />
<input type="hidden" />
</form>
<script type="text/javascript">
function show()
return $("form input[type=hidden]")
.replaceWith(function(i, el)
return "<input type=text>"
);


$.holdReady(true);
if (history.state !== null && history.state.clicked === true)
// show hidden fields
// if `history.state.clicked === true` ,
// replace `input type=hidden` with `input type=text`
show();
console.log(history);

else
// don't show hidden fields
console.log(history);

$.holdReady(false);

$(document).ready(function()

$("input[type=submit][value=Search]")
.on("click", function(e)
e.preventDefault();
if (history.state === null)
// do stuff
history.pushState("clicked":true);
// replace `input type=hidden` with `input type=text`
show();
console.log(history);
else
// do other stuff
;
);

);
</script>
</body>





share|improve this answer























  • I will give that a go. Thanks for your answer.

    – Neophile
    May 11 '15 at 16:25













5












5








5







Try utilizing $.holdReady() , history



<script src="jquery.js" type="text/javascript"></script>
</head>
<body>
<form method="POST">
<input type="text" name="name" value="" />
<input type="submit" value="Search" />
<input type="hidden" />
<input type="hidden" />
</form>
<script type="text/javascript">
function show()
return $("form input[type=hidden]")
.replaceWith(function(i, el)
return "<input type=text>"
);


$.holdReady(true);
if (history.state !== null && history.state.clicked === true)
// show hidden fields
// if `history.state.clicked === true` ,
// replace `input type=hidden` with `input type=text`
show();
console.log(history);

else
// don't show hidden fields
console.log(history);

$.holdReady(false);

$(document).ready(function()

$("input[type=submit][value=Search]")
.on("click", function(e)
e.preventDefault();
if (history.state === null)
// do stuff
history.pushState("clicked":true);
// replace `input type=hidden` with `input type=text`
show();
console.log(history);
else
// do other stuff
;
);

);
</script>
</body>





share|improve this answer













Try utilizing $.holdReady() , history



<script src="jquery.js" type="text/javascript"></script>
</head>
<body>
<form method="POST">
<input type="text" name="name" value="" />
<input type="submit" value="Search" />
<input type="hidden" />
<input type="hidden" />
</form>
<script type="text/javascript">
function show()
return $("form input[type=hidden]")
.replaceWith(function(i, el)
return "<input type=text>"
);


$.holdReady(true);
if (history.state !== null && history.state.clicked === true)
// show hidden fields
// if `history.state.clicked === true` ,
// replace `input type=hidden` with `input type=text`
show();
console.log(history);

else
// don't show hidden fields
console.log(history);

$.holdReady(false);

$(document).ready(function()

$("input[type=submit][value=Search]")
.on("click", function(e)
e.preventDefault();
if (history.state === null)
// do stuff
history.pushState("clicked":true);
// replace `input type=hidden` with `input type=text`
show();
console.log(history);
else
// do other stuff
;
);

);
</script>
</body>






share|improve this answer












share|improve this answer



share|improve this answer










answered May 9 '15 at 19:52









guest271314guest271314

1




1












  • I will give that a go. Thanks for your answer.

    – Neophile
    May 11 '15 at 16:25

















  • I will give that a go. Thanks for your answer.

    – Neophile
    May 11 '15 at 16:25
















I will give that a go. Thanks for your answer.

– Neophile
May 11 '15 at 16:25





I will give that a go. Thanks for your answer.

– Neophile
May 11 '15 at 16:25











1














Using localeStorage or sessionStorage seems to be the best bet.



Intead of saving the clicked variable in the globle scope store it this way:



if(localeStorage.getItem("clicked") === null)
localeStorage.setItem("clicked", "FALSE"); // for the first time

$(document).ready(function()

$("input[type='submit'][value='Search']").attr("onclick", "form.act.value='detailSearch';return true;");

var clicked = localeStorage.getItem("clicked") == "FALSE" ? "TRUE" : "FALSE";

localeStorage.setItem("clicked", clicked);

if (clicked == "TRUE")
// show hidden fields
else
// don't show hidden fields


);





share|improve this answer























  • I am intending on not using localstorage or sessionStorage preferably.

    – Neophile
    May 7 '15 at 9:33















1














Using localeStorage or sessionStorage seems to be the best bet.



Intead of saving the clicked variable in the globle scope store it this way:



if(localeStorage.getItem("clicked") === null)
localeStorage.setItem("clicked", "FALSE"); // for the first time

$(document).ready(function()

$("input[type='submit'][value='Search']").attr("onclick", "form.act.value='detailSearch';return true;");

var clicked = localeStorage.getItem("clicked") == "FALSE" ? "TRUE" : "FALSE";

localeStorage.setItem("clicked", clicked);

if (clicked == "TRUE")
// show hidden fields
else
// don't show hidden fields


);





share|improve this answer























  • I am intending on not using localstorage or sessionStorage preferably.

    – Neophile
    May 7 '15 at 9:33













1












1








1







Using localeStorage or sessionStorage seems to be the best bet.



Intead of saving the clicked variable in the globle scope store it this way:



if(localeStorage.getItem("clicked") === null)
localeStorage.setItem("clicked", "FALSE"); // for the first time

$(document).ready(function()

$("input[type='submit'][value='Search']").attr("onclick", "form.act.value='detailSearch';return true;");

var clicked = localeStorage.getItem("clicked") == "FALSE" ? "TRUE" : "FALSE";

localeStorage.setItem("clicked", clicked);

if (clicked == "TRUE")
// show hidden fields
else
// don't show hidden fields


);





share|improve this answer













Using localeStorage or sessionStorage seems to be the best bet.



Intead of saving the clicked variable in the globle scope store it this way:



if(localeStorage.getItem("clicked") === null)
localeStorage.setItem("clicked", "FALSE"); // for the first time

$(document).ready(function()

$("input[type='submit'][value='Search']").attr("onclick", "form.act.value='detailSearch';return true;");

var clicked = localeStorage.getItem("clicked") == "FALSE" ? "TRUE" : "FALSE";

localeStorage.setItem("clicked", clicked);

if (clicked == "TRUE")
// show hidden fields
else
// don't show hidden fields


);






share|improve this answer












share|improve this answer



share|improve this answer










answered May 6 '15 at 10:13









me_digvijayme_digvijay

2,99243468




2,99243468












  • I am intending on not using localstorage or sessionStorage preferably.

    – Neophile
    May 7 '15 at 9:33

















  • I am intending on not using localstorage or sessionStorage preferably.

    – Neophile
    May 7 '15 at 9:33
















I am intending on not using localstorage or sessionStorage preferably.

– Neophile
May 7 '15 at 9:33





I am intending on not using localstorage or sessionStorage preferably.

– Neophile
May 7 '15 at 9:33











-3














You could try this:



 $("input[type='submit'][value='Search']").click(function()
form.act.value='detailSearch';
clicked = true;
return true;
);





share|improve this answer

























  • When the page reloads, the still get the clicked value as false.

    – Neophile
    May 1 '15 at 12:16






  • 1





    @TheNewbie do you think you can pass a query string? with page refresh the current approach will not work.

    – renakre
    May 1 '15 at 12:19
















-3














You could try this:



 $("input[type='submit'][value='Search']").click(function()
form.act.value='detailSearch';
clicked = true;
return true;
);





share|improve this answer

























  • When the page reloads, the still get the clicked value as false.

    – Neophile
    May 1 '15 at 12:16






  • 1





    @TheNewbie do you think you can pass a query string? with page refresh the current approach will not work.

    – renakre
    May 1 '15 at 12:19














-3












-3








-3







You could try this:



 $("input[type='submit'][value='Search']").click(function()
form.act.value='detailSearch';
clicked = true;
return true;
);





share|improve this answer















You could try this:



 $("input[type='submit'][value='Search']").click(function()
form.act.value='detailSearch';
clicked = true;
return true;
);






share|improve this answer














share|improve this answer



share|improve this answer








edited May 9 '15 at 16:09









Downgoat

7,37733253




7,37733253










answered May 1 '15 at 12:16









renakrerenakre

5,26111549




5,26111549












  • When the page reloads, the still get the clicked value as false.

    – Neophile
    May 1 '15 at 12:16






  • 1





    @TheNewbie do you think you can pass a query string? with page refresh the current approach will not work.

    – renakre
    May 1 '15 at 12:19


















  • When the page reloads, the still get the clicked value as false.

    – Neophile
    May 1 '15 at 12:16






  • 1





    @TheNewbie do you think you can pass a query string? with page refresh the current approach will not work.

    – renakre
    May 1 '15 at 12:19

















When the page reloads, the still get the clicked value as false.

– Neophile
May 1 '15 at 12:16





When the page reloads, the still get the clicked value as false.

– Neophile
May 1 '15 at 12:16




1




1





@TheNewbie do you think you can pass a query string? with page refresh the current approach will not work.

– renakre
May 1 '15 at 12:19






@TheNewbie do you think you can pass a query string? with page refresh the current approach will not work.

– renakre
May 1 '15 at 12:19


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29986657%2fpersist-variables-between-page-loads%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto