guardar el valor de la variable y recuperarlo después de actualizar la página [duplicado]

I have a string value saved in to a variable, my webpage auto reloads after a certain process.. I need to know if I can get the value stored in that variable even after page refresh?

Im refreshing my web page using javascript code window.location.reload()

If not will it work if I take to server side script like php?

preguntado el 31 de julio de 12 a las 13:07

6 Respuestas

JavaScript:

  1. localStorage (HTML5 browsers only) - you could save it as a property of the page's local storage allowance

  2. save it in a cookie

  3. append the variable to the URL hash so it's retrievable via location.hash después de la actualización

PHP

  1. save it as a session variable and retrieve it over AJAX each time the page loads

  2. save it in a cookie (might as well do the JS approach if you're going to cookie it)

Any PHP approach would be clunky as you'd have to first send the value of the variable to a PHP script over AJAX, then retrieve it over AJAX after reload.

Respondido el 18 de junio de 21 a las 14:06

html5 browsers in the sense my browsers are uptodate..so it should work, right? - Da Silva

En ese caso localStorage es definitivamente el camino a seguir. - Mitia

one more thing.. how do I clear the localstorage value?? - Da Silva

conjunto: localStorage.setProperty(); remove: localStorage.removeProperty() - Mitia

You can store this as a $_SESSION variable.

session_start();

$myVar = null;

// some code here

if (!isset($_SESSION['myVar'])) {
    $_SESSION['myVar'] = "whatever";
} else {
    $myVar = $_SESSION['myVar'];
}

Respondido 31 Jul 12, 14:07

You will have to persist this variable in a Cookie/Webstorage/Session. Web pages are stateless.

Respondido 31 Jul 12, 14:07

yes you could save your variable on server side as session value or on client side in localstorage (o como cookie)

Respondido 31 Jul 12, 14:07

Cookies are your friend:

// Set a cookie or 2
document.cookie = 'somevar=somevalue';
document.cookie = 'another=123';

function getCookie(name)
{
    // Cookies seperated by ;   Key->value seperated by =
    for(var i = 0; pair = document.cookie.split("; ")[i].split("="); i++)
        if(pair[0] == name)
            return unescape(pair[1]);

    // A cookie with the requested name does not exist
    return null;
}

// To get the value
alert(getCookie('somevar'));

Respondido 31 Jul 12, 14:07

Using javascript you can store that variable into a cookie (the user must have cookies enabled), and then retrieve the cookie after.

The approach is something like this:

Para salvar:

function setCookie(c_name,value)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + 1);
var c_value=escape(value);
document.cookie=c_name + "=" + c_value;
}

Para recuperar:

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

Ref: http://www.w3schools.com/js/js_cookies.asp

Respondido 31 Jul 12, 14:07

mine is a simple string like "Market properties" .. - Da Silva

ok. you can still use cookies for that - dmmd

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.