67976

Is it possible to create a variable, that doesn't change on refresh the page, in javascript?

After I ask this question:

I was thinking about this solution:

    <li>Create a boolean flag inside the javascript script and make it true:
      <li><strong>true</strong> indicates refreshing the page using the browser.</li> <li><strong>false</strong> indicates refreshing the page using a button.</li> </ul></li> <li>Inside onclick attribute of the button, I set flag to false and call window.location.reload() to refresh the page.</li> <li>Inside onload attribute of <body> tag, I check the flag if it's true switch to Home tab else switch to Page1 tab where it contains the <div> tag I want to refresh its contents. After that, set flag to true.*</li> </ul>

      But unfortunately it doesn't work and flag is always true when it comes to onload attribute of <body> tag. Is it possible (somehow) to create a variable, that doesn't change on refresh the page, in javascript?

      *<sub>My page consistes of many tabs, first one is Home (where it should be shown when refreshing the page using the browser) and second one is Page1 (where it contains the <div> tag I want to refresh its contents and where it should be shown when refreshing the page using the button)</sub>

      Answer1:

      The browser is stateless, which means that everything on the page is refreshed each time the page loads.

      There are a few ways to store persistant information that you can access between requests:

        <li>Store the data you want in a cookie and read it when the page loads</li> <li>Pass data to the URL and read the current URL (including the data that you passed) when the page loads. For example: Redirect the browser to http://something.com/yourpage.html#flag=true if the user refreshed through the browser or redirect to http://something.com/yourpage.html#flag=false if the user clicked the button. On the onload of the page just read the current URL and see what the flag is.</li> <li>Use a cookie to create a session and store data on the server (technically this is very similar to the first option except you just store a token on the client and pass it to the server to retrieve data that was stored there)</li> </ul>

        Answer2:

        No. However, you can store the variable's value in a number of ways.

          <li>a cookie</li> <li>in the url (query string)</li> <li>hidden element on page set from a session variable.</li> </ul>

          Answer3:

          This sounds like a situation where you could use sessionStorage or localStorage in the browser and if the browser doesn't support that, you could fallback on cookies...

          Answer4:

          You can persist the data by using cookies, window.name, and window.location.hash key

Recommend

  • How to do a specific condition for escaped_fragment with rewrite rule in .htaccess
  • $_session variable
  • EnhanceWithin() is not working
  • How do I redirect with a Drop Down List, and not a Button?
  • How do I recognize a line break with a switch case that evaluates a char in Java?
  • How to add System.Windows dll to Visual Studio 2010 express?
  • WP7 difficulties binding data to listbox itemssource - won't refresh
  • if some function is not optimized does it mean that all functions where it is declared are not optim
  • Ruby 1.8.6 Array#uniq not removing duplicate hashes
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • Can my PDF ping my server when it is opened?
  • Configure Spring's MappingJacksonHttpMessageConverter
  • Outlines on links in IE9 remains when focus is changed
  • Hide HTML elements without javascript, only CSS
  • AppleScript : find open tab in safari by name and open it
  • saving file generated by TCPDF
  • Copy to all folders batch file?
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Play WS (2.2.1): post/put large request
  • Moving mysql files across servers
  • Android full screen on only one activity?
  • FileReader+canvas image loading problem
  • Get one-time binding to work for ng-if
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Volley JsonObjectRequest send headers in GET Request
  • Accessing IRQ description array within a module and displaying action names
  • Importing jscolor library in angular 2
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How to load view controller without button in storyboard?