HotIceCream
HotIceCream

Reputation: 2309

How to set variable once for different pages in one window

I want set boolean flag for current window. I will use this flag on all pages in this window. LocalStorage bad idea, becouse it set flag permanently for all windows. How can I do it?

Upvotes: 0

Views: 41

Answers (4)

mohamed-ibrahim
mohamed-ibrahim

Reputation: 11137

Use cookies:

set the cookie :

function setCookie(c_name,value,exdays)
{
  var exdate=new Date();
  exdate.setDate(exdate.getDate() + exdays);
  var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
  document.cookie=c_name + "=" + c_value;
}

Get the cookie:

function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
  {
  c_start = c_value.indexOf(c_name + "=");
  }
if (c_start == -1)
  {
  c_value = null;
  }
else
  {
  c_start = c_value.indexOf("=", c_start) + 1;
  var c_end = c_value.indexOf(";", c_start);
  if (c_end == -1)
  {
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}

Reference

Upvotes: 0

Joke_Sense10
Joke_Sense10

Reputation: 5412

You could use the window’s name window.name to store the information. This method is often used to modify the name of a window, after the window has been created. It only works as long as the same window/tab is used.

For more info: https://developer.mozilla.org/en-US/docs/Web/API/Window.name

Upvotes: 1

mohamed-ibrahim
mohamed-ibrahim

Reputation: 11137

Like jQuery, jQuery attached to window and work as a global variable that can access from any window you can do something like...

window.my_flag = false

Upvotes: 1

user2009750
user2009750

Reputation: 3197

Use PHP or JSP session and then retrieve your value in any page using AJAX.

You should have an ajax request to initialize your value first. Then you should set/update/read your boolean value accordingly using AJAX.

Let me know if you need details.

Upvotes: 0

Related Questions