Hi everyone,
I was trying to achieve a functionality for drop down box to remember its selected value if the page reloads. I saw some code on this forum using onload method. Title of the thread is "Javascript generated drop down menu " within the last two weeks.

Problem is I dont have access to the head or body section. What I do is I create a string which has the javascript and html script and pass that to the control to be displayed in the browser. There is a globaldhtmlcode property where you can write your own code. I tried using window.onload = functionName; but no luck.

Can the same functionality be achieved using cookies?

Any ideas
Thanks,
Dhruv.

Yes, you can use a cookie to persist a value, but you'll still need JavaScript in order to set/get cookie values.

First, you need to test if your environment will let you include JavaScript elements. A simple alert will be a good test.

If you can post:

<script>window.alert("Hello world!");</script>

to your globaldhtmlcode property, does it work?

Thanks for the reply. Javascript works, that I know. There is a global included js file which has some cookies functions I might be able to reuse. Just dont know how to.

function parseMonth()
{
var q = unescape(location.search);
q = q.substring(11, q.length);
if (q != "")
{
document.getElementById("ParmMonth").options.selectedIndex = q;
}
}
EDIT: This code works for sure.


This is the code I have used. But I dont know how to pass the query string to have some value in location.search. And I will also have to parse it because there are other values in it as well. What do you suggest.

EDIT: I now need to find a way to set a cookie.

Thanks,
Dhruv.

When you say you don't know how to pass the QueryString, what exactly do you mean? I'm assuming that you know what the term "QueryString" means:

http://www.tgreer.com/resources.php?style=0

Everything after the question mark is the "QueryString". The JavaScript "location.search" object returns that to you. You have to parse it, using string functions, to get the particular value you need.

I have written something that does exactly what (I think) you're describing in another thread. It takes a value from the QueryString, and uses it to set the selected value in a Select/Option element. http://www.daniweb.com/techtalkforums/thread27049.html

But I'm guessing that you can't really USE a querystring, can you? Because that would imply you already know the value to pass in. You don't, do you? That's why you need to find a way to store the value in cookie, and get the value out of the cookie.

Is that correct?

Right on my friend. I dont know the value selected in the drop down, so I dont know what to pass in the query string.
I was filddling with cookies,

function cookieUpdate(field)
{
    SetCookie(field, document.all(field).value);

}

function fieldUpdate(field)
{
    if(Get_Cookie(field))
        document.all(field).value = Get_Cookie(field);

    if(document.all(field).value == "undefined")
        document.all(field).value = "";
}


function SetCookie(name,value) 
{
        var jSessionID = getJSESSIONID();
        if ((name == "mroWhere") || (name == "mroCurrent") || (name == "mroSelect") || (name == "localTZ")) {
            document.cookie = name + "=" +value + " ; path=/";
        } else {
            document.cookie = name + "=" +escape(value) + " ; path=/";
        }
        if (!(document.cookie.indexOf("JSESSIONID=") > -1)) {
            document.cookie = "JSESSIONID=" +escape(jSessionID) + "; path=/";
        }

}

These are some of the functions in my global js file, I was trying to use them but I get object required error.

EDIT: This is how I use the cookie:

document.cookie = 'month' + '=' +escape(document.getElementById('ParmMonth').options.selectedIndex) + ' ; path=/';

And in my global js file,

month=Get_Cookie("ParmMonth");
function parseMonth()
{ 
document.getElementById("ParmMonth").options.selectedIndex = month ;

}

this.onLoad = parseMonth;

Thanks a lot for your responses.
Dhruv.

The problem is probably caused by "document.all", which is browser-specific. Do you have enough information to solve the problem yourself? You need to find some cross-browser JavaScript code to get/set cookies. You can probably find hundreds of examples by doing a web search.

Then, you need to pass that cookie's value into your select box, using the techniques found in the thread I referenced.

None of my code points to document.all really. But I will do a search for cookie examples and see what to do.
Thanks a lot.
Dhruv.

Hey,
I have also come across this problem in my projects... this is what I do.

// global code in your body...

// ensure old onload code is preserved.
var oldWindowOnload = window.onload;
window.onload = function(e){
   // execute old onload code...
   if(oldWindowOnload) oldWindowOnload(e);
   /* what ever you want to add to the onload function here */
}

Thanks a lot, will try as soon as i get a chance

What I am trying to do is store the value of the option selected in a drop down in onchange event, and set the value of the drop down in onload event.
Tried lot of variations but cant seem to make the code work.
Thanks,
Dhruv

I think I am very close. Below code is working. I am passing the value of selectedindex in dd as onchange event from the drop down. setDropdown is my onload event. If I hard code it, it works, but if I use
document.getElementById('ParmMonth').options.selectedIndex = cookiedValue, it doesnt.

Any ideas.
Thanks,
Dhruv


function setDropDown()
{
document.getElementById('ParmMonth').options.selectedIndex = 2
alert("set" & cookiedValue)
}

function changeDropDown(dd)
{

cookiedValue = dd
alert("dD");
alert(dd);
alert("value");
alert(cookiedValue);
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.