Jump to content

How to memorize the value of a variable in JavaScript ?


tmyonline

Recommended Posts

Hi guys,

 

I need to keep the value of a variable memorized so that I can use it the next time around.  I know how to do it in PHP but don't know how to in JavaScript.  In PHP, I use the $_SESSION variable in conjunction with the declaration of session_start().  How would I do it in JavaScript ?  Thanks so much guys.

Link to comment
Share on other sites

You can't keep javascript from one page to another, you can change only part of the page by use of ajax to 'fake' it.. or use frames.

 

Unlike PHP javascript is fully clientside, and thus once the page is reloaded/changed to another page you're off to a 'fresh start'.

Link to comment
Share on other sites

depends on how you are going from page to page.  If you're clicking form submit buttons, you can populate a hidden field in the form and use js to grab it on the next page.  Or if by a link, you can pass it in the url as a query string parameter and use js to grab it from the query string.  Or if the user has cookies enabled, you can set a cookie and use js to grab the cookie value (as Dj Kat mentioned).

Link to comment
Share on other sites

Thanks guys for your prompt response.

 

By the way, there is a bug in my program which I spent the whole Sunday  afternoon and have not been able to find out.  The following code will display an option that allows someone to attach a document.  There is a "+" sign that allows him/her to add additional attachments.  Upon clicking the "+" sign, the "-" sign will appear in case he/she wants to remove a browse option.  Rightnow, the plus and minus signs work ok accept that when there are two browse fields and if you click the minus button, the whole thing will be gone.  If you could help me figure out this bug, I greatly appreciate it.

 

As a related problem, somehow, this thing does not work if my html is nested in a form.  I don't get it.  Below is my program:

 

 

<html>
<head>
<link rel="stylesheet" type="text/css" href="css/outage.css" />
<script type="text/javascript">
var numDocs;

function getCookie(cookieName) {
  if (document.cookie.length>0) {
    c_start = document.cookie.indexOf(cookieName + "=");
    if (c_start != -1) {
      c_start = c_start + cookieName.length + 1;
      c_end   = document.cookie.indexOf(";",c_start);
      if (c_end == -1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}


function setCookie(cookieName, value, expiredays) {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + expiredays);
  document.cookie = cookieName + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}


function addAttachments() {
  numDocs = getCookie('numDocs');
  if (numDocs != null && numDocs != "") {
    numDocs++;
    setCookie('numDocs', numDocs, 30);
    numDocs = getCookie('numDocs');
    for (var i = 0; i < numDocs; i++) {
      var tr = document.createElement('tr');
      var td1 = document.createElement('td');
      td1.setAttribute('width', '120px');
      td1.setAttribute('align', 'right');
      text = document.createTextNode('Document ' + numDocs + ':');
      td1.appendChild(text);

      var td2    = document.createElement('td');
      var input  = document.createElement('input');
      input.type = "file";
      input.name = "doc_" + numDocs;
      input.size = "40";
      td2.appendChild(input);

      tr.appendChild(td1);
      tr.appendChild(td2);

      // insert additional fields between the first field and the plus Icon
  var plusIcon = document.getElementById('plus');
      plusIcon.parentNode.insertBefore(tr, plusIcon);

      // add the minus/removal button
      if (numDocs == 2) {
        var img    = document.createElement('img');
        img.setAttribute('src', '/outage/images/remove.png');
        img.setAttribute('height', '16px');

        var button = document.createElement('button');
        button.setAttribute('id', 'rightButton');
        button.setAttribute('onclick', 'removeAttachments()');
        button.appendChild(img);

        var leftButton = document.getElementsByTagName('button')[0];
        leftButton.parentNode.appendChild(button);
      }

      // put together the pieces to form the table
      var table = document.getElementById('attach');
      table.appendChild(plusIcon)[1];

      return table;
    }
  }
}


function removeAttachments() {

  var lastChild = document.getElementById('attach').lastChild;
  document.getElementById('attach').removeChild(lastChild);
  var lastChild = document.getElementById('attach').lastChild;
  document.getElementById('attach').removeChild(lastChild);

  numDocs--;

  var test = document.getElementById('attach');
  var numChildren = test.getElementsByTagName('tr').length;

  alert("numDocs: " + numDocs + ", numChildren: " + numChildren);

  // add back plus icon
  if (numDocs > 1) {
    var trIcon  = document.createElement('tr');
    var td1Icon = document.createElement('td');
    var td2Icon = document.createElement('td');
    var text    = document.createTextNode('');

    var button1 = document.createElement('button');
    var button2 = document.createElement('button');
    button1.setAttribute('onclick', 'addAttachments()');
    button2.setAttribute('onclick', 'removeAttachments()');

    var img1 = document.createElement('img');
    var img2 = document.createElement('img');
    img1.setAttribute('src', '/outage/images/add.png');
    img1.setAttribute('height', '16px');
    img2.setAttribute('src', '/outage/images/remove.png');
    img2.setAttribute('height', '16px');

    button1.appendChild(img1);
    button2.appendChild(img2);

    td1Icon.setAttribute('class', 'R');
    td1Icon.appendChild(text);
    td2Icon.setAttribute('class', 'L');
    td2Icon.appendChild(button1);
    td2Icon.appendChild(button2);

    trIcon.setAttribute('id', 'plus');
    trIcon.appendChild(td1Icon);
    trIcon.appendChild(td2Icon);
  }

  var table = document.getElementById('attach');
  table.appendChild(trIcon);

  setCookie('numDocs', numDocs, 30);

  return table;
}
</script>
</head>
<body onload="setCookie('numDocs', 1, 30)">
<!--
<form id="upload" action="" method="post" enctype="multipart/form-data">
-->
  <table id="attach">
    <tr>
  <td style="width: 120px; text-align: right;">Document 1:</td>
  <td class="L"><input type="file" name="doc_1" size="40" /></td>
    </tr>
    <tr id="plus">
  <td class="R"></td>
  <td class="L"><button onclick="addAttachments();"><img src="/outage/images/add.png" height="16" /></button></td>
    </tr>
  </table>
<!--</form>-->
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.