User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jul 2005
Posts: 10
Reputation: shahdhruv is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
shahdhruv shahdhruv is offline Offline
Newbie Poster

Problem using onload

  #1  
Jul 11th, 2005
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Posts: 1,590
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 34
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Problem using onload

  #2  
Jul 11th, 2005
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?
Reply With Quote  
Join Date: Jul 2005
Posts: 10
Reputation: shahdhruv is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
shahdhruv shahdhruv is offline Offline
Newbie Poster

Re: Problem using onload

  #3  
Jul 12th, 2005
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.
Reply With Quote  
Join Date: Dec 2004
Posts: 1,590
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 34
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Problem using onload

  #4  
Jul 12th, 2005
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?
Reply With Quote  
Join Date: Jul 2005
Posts: 10
Reputation: shahdhruv is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
shahdhruv shahdhruv is offline Offline
Newbie Poster

Re: Problem using onload

  #5  
Jul 12th, 2005
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.
Reply With Quote  
Join Date: Dec 2004
Posts: 1,590
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 34
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Problem using onload

  #6  
Jul 12th, 2005
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.
Reply With Quote  
Join Date: Jul 2005
Posts: 10
Reputation: shahdhruv is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
shahdhruv shahdhruv is offline Offline
Newbie Poster

Re: Problem using onload

  #7  
Jul 12th, 2005
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.
Reply With Quote  
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: Problem using onload

  #8  
Jul 17th, 2005
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 */
}
Reply With Quote  
Join Date: Jul 2005
Posts: 10
Reputation: shahdhruv is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
shahdhruv shahdhruv is offline Offline
Newbie Poster

Re: Problem using onload

  #9  
Jul 18th, 2005
Thanks a lot, will try as soon as i get a chance
Reply With Quote  
Join Date: Jul 2005
Posts: 10
Reputation: shahdhruv is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
shahdhruv shahdhruv is offline Offline
Newbie Poster

Re: Problem using onload

  #10  
Jul 19th, 2005
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 8:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC