views:

159

answers:

5

I am using Javascript to store values in a stack, the problem is that the stack value remains active until the page is refreshed.
When the page is refreshed the value in the stack value gets set to 'Undefined'.

Is it better to implement a place to permanently store the stack values? Then even if the page is refreshed the old stack value should be able to be retrieved.

How can I implement the below stack snippet, in a PHP script?

   var arr = new Array();
   var pushed=arr.push(id);  // for pushing into stack  
   arr.splice(0,arr.length); // for deleting stack

How to I refer to the PHP stored values in Javascript?

A: 

Not entirely sure what you're asking
If you want to implement the snippet using an object, try something like this:

var obj = {};
obj[key] = id; // to add
obj = {};      // to remove all items
del obj[key];  // to remove 1 item
cobbal
+4  A: 

If I interpret your question correctly, you're asking whether javascript objects can persist beyond a page refresh?

Unfortunately, when a new page is loaded, all javascript objects are lost. They don't survive a page refresh.

I would suggest using cookies. I'm not dedicated enough to write the code for you right now, but generally:

When you push/pop from the stack, persist the stack to a cookie that's saved on the client's machine. When the page loads for the first time (or refreshes in your example), look for that cookie and write the persisted stack to your javascript object.

Have a look at these links to help with persisting to cookies from javascript:

  1. http://codeinthehole.com/archives/6-Active-record-javascript-objects-using-cookies.html
  2. http://www.quirksmode.org/js/cookies.html
  3. http://www.w3schools.com/JS/js_cookies.asp
Damovisa
+1  A: 

Assuming that you want certain values javascript variables to persist between page loads, you can either do what Damovisa suggested or simply use a hidden field to store your stack values in some form. Maybe a comma delimited string or something like that.

The advantage is obvious. You're program won't depend on the client's browser to have enabled cookies.

As an alternative, if its possible, you can maintain your stack in some server side variable and then dynamically insert that value into javascript when the page is rendered.

It will be very helpful if you can give us more detail about your program.

Edit:

If you want to values in PHP variables to reflect in your javascript, embed PHP tags within your javascript.

Eg.

<script>
var str = "<?php echo $my_str; ?>";
//do something with str
</script>

Be sure to include the quotes ("), else the value in the string will be considered a token by javascript and might throw an error.

LVS
+1  A: 

You could serialize the object and post it to the next page with JavaScript in the URL and parse it on the page from the URL. This is probably not the best way to do it, but since you're asking, I assume you cannot use any backend technology to permanently store data.

simon
+2  A: 

Another way to solve this is to not refresh the page but reload its body or a form or a div element using ajax. jQuery makes this especially simple.

$('body').load(url);

That way you can have persistence of script values between updates

Scott Evernden