•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 402,750 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,442 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 4082 | Replies: 10
![]() |
•
•
Join Date: Jul 2005
Posts: 10
Reputation:
Rep Power: 4
Solved Threads: 1
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.
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:
to your globaldhtmlcode property, does it work?
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?
•
•
Join Date: Jul 2005
Posts: 10
Reputation:
Rep Power: 4
Solved Threads: 1
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.
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:
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?
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?
•
•
Join Date: Jul 2005
Posts: 10
Reputation:
Rep Power: 4
Solved Threads: 1
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.
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.
Then, you need to pass that cookie's value into your select box, using the techniques found in the thread I referenced.
•
•
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation:
Rep Power: 4
Solved Threads: 3
Hey,
I have also come across this problem in my projects... this is what I do.
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 */
}![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- onLoad commands (HTML and CSS)
- Page Opening - Cache Problem? (JavaScript / DHTML / AJAX)
- How can fix the problem? (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Forcing a script to load in Safari -- help needed
- Next Thread: setting value is not sticking



Linear Mode