cancel
Showing results for 
Search instead for 
Did you mean: 

Reading Cookie into Dropdown

Former Member
0 Kudos

Hi SDN,

Can anyone plz tell me how to set the selection attribute of a dropdown with the cookie from the browser??

<hbj:dropdownListBox

id = "myDropdownListBox1"

disabled = "false"

model = "myBean.model"

selection="readCookie(name)"

jsObjectNeeded = "TRUE"

onSelect="onProcess"

onClientSelect = "raiseMyEvent()">

</hbj:dropdownListBox><br>

readCookie returns a string from the cookie.

But it seems itz not reading though i can alert and see that the function returns the string...

Two pages have the same iview with a dropdown..

I want the second iview to read the cookie set by the first iview with the value selected in the dropdown.

This helps me in showing the same selection in the dropdown on both pages.

Any hints are gr8ly appreciated..

Thanks.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Explorer,

Here's a html example how you could do that:

<html>
<head>
<script type="text/javascript">

/* HERE YOU SHOULD ADD YOUR READ/WRITE COOKIE */
/* AND THEN PASS THE VALUE FROM COOKIE        */
/* TO VARIABLE valueFromCookie                */


function parseDropdown()
{
  var valueFromCookie = 1;
  if (valueFromCookie != "")
  {
    document.getElementById("myDropdownListBox1").options.selectedIndex = valueFromCookie;
  }
}
</script>
</head>
<body onload="parseDropdown();">
<form>
<select id="myDropdownListBox1">
<option value="" selected="selected">Select a Continent</option>
<option value="Africa">Africa</option>
<option value="Antarctica">Antarctica</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="Europe">Europe</option>
<option value="North America">North America</option>
<option value="South America">South America</option>
</select>
</form>
</body>
</html>

So the important stuff in this code is this part of javascript:

<b>document.getElementById("myDropdownListBox1").options.selectedIndex = valueFromCookie;</b>

This will set the selected value for form which id is <i>myDropdownListBox1</i>

You should write a number into your cookie. Play with the

<b>var valueFromCookie = 1;</b> and you'll see how it works. If I understood you right you already had figure out the writing cookie part?

Hope this helps,

Ville

Former Member
0 Kudos

Thanx for the code,

It seems poral doesn't like it.

It throws an error, that the object doesn't support the property.

when i comment the below line the error won be displayed..

document.getElementById("myDropdownListBox1").options.selectedIndex = valueFromCookie;

But can u like help me with the server side setting of the cookies??Thanx

Former Member
0 Kudos

Hi Explorer,

Sorry about a bit late reply but here's example how to write the cookie and then read the cookie value to the dropdownmenu:

<html>
<head>
<script type="text/javascript"> 
 
function parseDropdown()
{
  var valueFromCookie = getCookie('myDropdownListBox1');
  if (valueFromCookie != "")
  {
    document.getElementById("myDropdownListBox1").options.selectedIndex = valueFromCookie;
  }
}

/* Get Cookie */
function getCookie(NameOfCookie)
{ 
  if (document.cookie.length > 0) 
  { 
    begin = document.cookie.indexOf(NameOfCookie+"="); 
    if (begin != -1) 
    { 
      begin += NameOfCookie.length+1; 
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(begin, end)); 
    } 
  }
  return null; 
}

/* Set Cookie */
function setCookie() 
{
  NameOfCookie = "myDropdownListBox1";
  value = document.getElementById("myDropdownListBox1").options.selectedIndex;
  expiredays = 365;
  var ExpireDate = new Date ();
  ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
  document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

/* Delete Cookie */
function delCookie (NameOfCookie) 
{ 
  if (getCookie(NameOfCookie)) 
  {
  document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
} 

</script>
</head>
<body onload="parseDropdown();">
<form>
<select id="myDropdownListBox1" onChange="setCookie(); return false">
<option value="" selected="selected">Select a Continent</option>
<option value="Africa">Africa</option>
<option value="Antarctica">Antarctica</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="Europe">Europe</option>
<option value="North America">North America</option>
<option value="South America">South America</option>
</select>
</form>
</body>
</html>

Regards,

Ville

Former Member
0 Kudos

Thank you.

But iam working on HTMLB Dropdown..can u post code modified to that scenario..

Thanx a ton!