0

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 the stack
   arr.splice(0,arr.length); // For deleting the stack

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

1
  • All three answers that have been posted so far start with mentioning the fact that your question is not really clear. Could you please try to rephrase your question and sum up the actual problem you have? This would definately help in getting a better answer. Mar 19, 2009 at 7:17

5 Answers 5

4

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 pages to help with persisting to cookies from JavaScript:

  1. David Winterbottom's links
  2. Cookies
  3. JavaScript Cookies
3

Another way to solve this is to not refresh the page, but reload its body, 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.

1

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.

1

Assuming that you want certain values in 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 it's possible, you can maintain your stack in some server side variable and then dynamically insert that value into JavaScript code when the page is rendered.

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

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

For example:

<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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.