flat assembler
Message board for the users of flat assembler.

Index > Heap > [HTML]Persistence of hidden input form field

Author
Thread Post new topic Reply to topic
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Incredibly bad forum for asking this but this is where I belong to and also I know some people here code HTML for food Wink

Does anyone know how safe is storing data in the hidden input form's value and expecting it to be there after the user press the back button BUT the browser is not kind enough of downloading the page from the server again (behavior I'm very grateful of actually)?

What I need is to store some small state about a simple control that is JavaScript-generated, but after trying many ways to use variables, it seems them are always reset when you go back but this is not happening with hidden input form's values. I'm storing this state in the server-side of the application from the hidden input and then I redirect the user to the target page, but since the browsers are not re-downloading the page when the user presses the browser's Go Back button, it's been only useful for some browsers that may what to refresh the page upon going back (i.e. browsers are not only caching the page but also the content of the hidden input that was left by the JavaScript code)

So, is this documented somehow or I'm just being extremely lucky by having this working for IE 7 and 8, Firefox and Opera?

PS: The hidden input is like this: <input type="hidden" name="state" id="state" /> (i.e. attribute "value" is not there so that is probably helping in not resetting the value when the page is reloaded without downloading it again).
Post 21 Sep 2009, 04:10
View user's profile Send private message Reply with quote
ass0



Joined: 31 Dec 2008
Posts: 521
Location: ( . Y . )
ass0
Quote:
code HTML for food
xD

si te entendí bien, solo necesitas usar ajax para asegurarte al 100%, ejemplo:

Code:
var state = 0;

if (state < 1){
 //recuperar datos del servidor con ajax, asi no necesitas que el usuario presione "f5" o refresque de alguna manera.
state = 1;
}
    
Post 21 Sep 2009, 04:42
View user's profile Send private message Reply with quote
ass0



Joined: 31 Dec 2008
Posts: 521
Location: ( . Y . )
ass0
si no sabes ajax me dices y te doy la solución completa =D
Post 21 Sep 2009, 04:43
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Es que en realidad quisiera tomar ventaja de la persistencia que me están dando los navegadores porque es más rápido. Todo el estado que estoy guardando es donde estaba el DIV para volverlo a poner en la misma posición.

He visto en algunos foros que además del truco que estoy usando también podría usar un textarea oculto. El problema que en ningún caso encuentro algo que diga que es estándar pero el hecho es que de verdad funciona (el hidden input al menos). Lo que es JavaScript se resetea todo, incluso se vuelve a llamar a la función load() que tengo (en la cual también veo si hay algún estado guardado).

Estoy usando Prototype y el código es más o menos así:

Code:
function load(){
.
.
.
      cachedState = $('state').value;

      if (state || cachedState && (state = cachedState.evalJSON())) {

         coordsDiv.setState(state.coordsDivState)
.
.
.
      }
}

function save() {
.
.
.
   $('state').value = $H({
      coordsDivState: coordsDiv.getState(),
.
.
.
   }).toJSON();
}    

La variable state está declarada globalmente y si hay estado almacenado en el lado del server entonces viene inicializada, caso contrario queda indefinida y entonces se prueba rescatando el estado "cacheado".

Well... Now all again in English... This is the last time I write in Spanish Very Happy

Actually I would like to take advantage of the persistence the browsers are giving to me because it is faster. All the state I'm saving is where a DIV was to put it back at the same place it was.

I've saw in some forums that besides the trick I'm using I could also use a hidden textarea. The problem is that I can't find a single case where it is stated that it is standard but it indeed works (the hidden input at least). All the JavaScript stuff gets reset, even the load() function I have is called again (in which I also see if I have a state stored).

I'm using Prototype and the code is more or less this:

(Look the code above)

The variable state is declared globally and if there is state stored in the server side then it comes initialized, otherwise it is left undefined and then it rescuing the "cached" state is attempted.

--- NO SPANISH PART FOR THIS

What I really want to know if it is a documented fact what I'm doing or if this is just plain luck that it works. Alternatives are also welcomed though, so thanks for the AJAX tip Wink
Post 21 Sep 2009, 05:17
View user's profile Send private message Reply with quote
ass0



Joined: 31 Dec 2008
Posts: 521
Location: ( . Y . )
ass0
Si no te simpatiza ajax tambien puedes usar cookies.

La ventaja es que te ahorras ancho de banda y la contra es que en php los headers pueden joderte, además hay usuarios paranoicos que deshabilitan javascript, no los culpo (el tema de los heapSprays); pero para mí es demasiada paranoia xD.
Post 21 Sep 2009, 05:24
View user's profile Send private message Reply with quote
ass0



Joined: 31 Dec 2008
Posts: 521
Location: ( . Y . )
ass0
wooot me ganaste; pero creo que adivine tu respuesta xD
Post 21 Sep 2009, 05:25
View user's profile Send private message Reply with quote
ass0



Joined: 31 Dec 2008
Posts: 521
Location: ( . Y . )
ass0
btw. what exactly are you trying to maintain as persistent data, integers, strings, arrays, what is the length? more than 4kb?
Post 21 Sep 2009, 05:40
View user's profile Send private message Reply with quote
ass0



Joined: 31 Dec 2008
Posts: 521
Location: ( . Y . )
ass0
I think that cookies way is the standards way =D.

why?

because they are made to store persistent data, not only when user press back button, but when users aborts session (close browser).

If your audience don't have cookies enabled then your trick will be the solution.
Post 21 Sep 2009, 05:52
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
The problem with using cookies is that them are of limited size and I plan to store more state than I'm storing now and I could lost it or even left the page inaccessible due to shitty transparent proxies on the path that can cope with such a simple thing... (I start to loss access to this forum when the phpbb2mysql_t cookie gets too long but it goes fine when I enter here via HTTPS except for the fact that the visited threads are no longer marked as such).

JavaScript must be unconditionally enabled, otherwise the DIV couldn't been created in the first place Laughing

What I'm saving is a small object (for now but I don't expect to send more than 256 KB in the future).
Post 21 Sep 2009, 05:59
View user's profile Send private message Reply with quote
ass0



Joined: 31 Dec 2008
Posts: 521
Location: ( . Y . )
ass0
<= 256kb?? what kind of object is that?

256kb == 262144 ascii chars dood!!

are you saving binary/base64 data?

some kind of game/graphical webapp?
Post 21 Sep 2009, 06:09
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
That is just an upper bound that probably will never be approached in practice (currently impossible because there is nothing requiring to save that much), but I know cookies are not the way to go because the maximum size will probably be reached.

The real alternative so far is using AJAX as you suggested, but I would like to know if the current method is really unsafe or not.
Post 21 Sep 2009, 06:17
View user's profile Send private message Reply with quote
ass0



Joined: 31 Dec 2008
Posts: 521
Location: ( . Y . )
ass0
Cookies can store up to 4kb and you can create as much as you want, the phpbb issue that you said is just because the phpbb design.
Cookies work very well even with a chain of proxies if they are created by javascript rather than headers way.
Post 21 Sep 2009, 06:32
View user's profile Send private message Reply with quote
ass0



Joined: 31 Dec 2008
Posts: 521
Location: ( . Y . )
ass0
no difames mis galletas! xD
Post 21 Sep 2009, 06:40
View user's profile Send private message Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete
You could use SESSIONS, these are a mixup of cookies and server stored data. The cookie only holds the id of the user, the server stored data can hold anything. Then, on every page-request, the hidden input field could get generated via the data in the session.

In addition, you could use AJAX to transfer data from the server to JavaScript. I used SAJAX, when needed: www.modernmethod.com/sajax/
Post 21 Sep 2009, 09:47
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
Hidden IFRAME on all your pages that loads current state into a javascript object.

In your page
Code:
window.stateObject = null;
//load your iframe
alert(window.stateObject.magicValueArray);
    

In your iframe
Code:
window.parent.stateObject = <?php echo @jsonFromServer; ?>
window.parent.stateObject = <%= jsonFromServer %>
    

Always load your IFRAME with a unique value in the SRC so that it doesn't get cached.
Code:
document.getElementById('myStateIFRAME').src = STATE_URL + "?nocache=" + (new Date()).getTime();
    
Post 21 Sep 2009, 17:06
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Guys, I think I found something that backs my method: http://code.google.com/p/chromium/issues/detail?id=5353

So, it is a bug not keeping the form inputs intact! Very Happy

Thanks for all the proposed methods, if I found some trouble with the current method I'm using I'll switch to the one that better fits my needs.
Post 21 Sep 2009, 18:39
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.