Solved

Javascript to write cookie for state selector used on another page

Posted on 2011-03-08
4
292 Views
Last Modified: 2012-06-27
This is the information that I received, but being a novice do not know what code to add to the attached javascript file or the page where the state is selected to make it work. Can anyone help me? The idea is that the person selects a state on one page that takes them to another URL.  On that page, the user clicks another link that takes them to a page with a pre-selected state in the result based on the cookie initially selected.

Refer to: http://jason.pureconcepts.net/articles/javascript_cookie_object
- In the function setDDValue, a cookie can be set in the closure between line 117 and 124
- This cookie would have the same value as the variable 'value', passed in the function (i.e.: 'value' would be something like "Alaska")
- Since the Prototype library cannot "click" things programmatically, an event listener for a custom event needs to be created in order for it to work. Copy line 93, and paste it immediately after, and change it with a new custom even name, eg.:

Event.observe(option, 'stateSelected', (observeOption), false);

On the page that needs to be smart enough to auto select the city:

- Read the cookie stored
- If cookie exists, fire the stateSelected event for the corresponding state link, ie: $('rrtaa67').fire('stateSelected'); where rrtaa67 is the ID name of the link in custom dropdown.
- This must be after the page has done loading, so bind it to the document load event.
dropdown.js
0
Comment
Question by:BearWebContent
  • 2
4 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 35085625
You can probably use the Animate method from jQuery and the Comlete method. Once the animation is complete you could inject the link into the div at that point so the slide happens completely before the text appears.

http://api.jquery.com/animate
0
 
LVL 28

Accepted Solution

by:
Pravin Asar earned 500 total points
ID: 35085720
Here is  a simpler version of cookie management code.

http://javascriptjournal.com/ws/?p=54


The code is well documented and easy to use with your application.

On page load, you have do is

function SetValues() {
        var selValue =GetCookieValue('state');
         // get the form fields
        if (selValue.length) {
       document.FormName.FieldName.value =  selValue;
       }
}
0
 
LVL 15

Expert Comment

by:SRigney
ID: 35085942
Sorry, wrong window.

I don't think you have to go through the code to click the state dropdown.  You should be able to set the selected index to the value that matches the state stored in the cookie without needing to click on it.

Based on the info you have though.
On both the page you want to save the cookie, and the page you want to read the cookie you need to include the script from http://jason.pureconcepts.net/articles/javascript_cookie_object

At line 119 I think you need to add
                  Cookie.init({name: 'mydata'}, {state: Input.value, x: 0});


Then on the page that you want to set the value add the following javascript
document.observe("dom:loaded", function() {
$("selectboxTopreSet").value(  Cookie.getData('state') );
 $('selectboxTopreSet').fire('stateSelected');
});
0
 

Author Comment

by:BearWebContent
ID: 35088562
Scenario:
I set the cookie using the form code below.  Selecting a state from the drop-down takes me to xyz.html (or wherever else).  On xyz.html is a link that opens a shadow box with another state selector that should be automatically open to the right state information. (See state-selector-xyz.html)

Is this clearer?  

 state-selector-xyz.html state-selector-xyz.html
<form>
			<div id="compare-select" class="select hide-dropdown">
				<div class="wrapper">
					<a onClick="return observeOpener(this);" href="" class="dd-opener">Select a state</a>
					<a onClick="return observeOpener(this);" href="" class="dd-opener pulldown-arrow"></a>
				</div>
<ul class="selectbox"><li><a href="??" value="AL">Alabama</a></li><li><a href="??" value="AK">Alaska</a></li><li><a href="??" value="AZ">Arizona</a></li><li><a href="??" value="AR">Arkansas</a></li><li><a href="??" value="CA">California</a></li><li><a href="??" value="CO">Colorado</a></li><li><a href="??" value="CT">Connecticut</a></li><li><a href="??" value="DE">Delaware</a></li><li><a href="??" value="FL">Florida</a></li><li><a href="??" value="GA">Georgia</a></li><li><a href="??" value="HI">Hawaii</a></li><li><a href="??" value="ID">Idaho</a></li><li><a href="??" value="IL">Illinois</a></li><li><a href="??" value="IN">Indiana</a></li><li><a href="??" value="IA">Iowa</a></li><li><a href="??" value="KS">Kansas</a></li><li><a href="??" value="KY">Kentucky</a></li><li><a href="??" value="LA">Louisiana</a></li><li><a href="??" value="ME">Maine</a></li><li><a href="??" value="MD">Maryland</a></li><li><a href="??" value="MA">Massachusetts</a></li><li><a href="??" value="MI">Michigan</a></li><li><a href="??" value="MN">Minnesota</a></li><li><a href="??" value="MS">Mississippi</a></li><li><a href="??" value="MO">Missouri</a></li><li><a href="??" value="MT">Montana</a></li><li><a href="??" value="NE">Nebraska</a></li><li><a href="??" value="NV">Nevada</a></li><li><a href="??" value="NH">New Hampshire</a></li><li><a href="??" value="NJ">New Jersey</a></li><li><a href="??" value="NM">New Mexico</a></li><li><a href="??" value="NY">New York</a></li><li><a href="??" value="NC">North Carolina</a></li><li><a href="??" value="ND">North Dakota</a></li><li><a href="??" value="OH">Ohio</a></li><li><a href="??" value="OK">Oklahoma</a></li><li><a href="??" value="OR">Oregon</a></li><li><a href="??" value="PA">Pennsylvania</a></li><li><a href="??" value="RI">Rhode Island</a></li><li><a href="??" value="SC">South Carolina</a></li><li><a href="??" value="SD">South Dakota</a></li><li><a href="??" value="TN">Tennessee</a></li><li><a href="??" value="TX">Texas</a></li><li><a href="??" value="UT">Utah</a></li><li><a href="??" value="VT">Vermont</a></li><li><a href="??" value="VA">Virginia</a></li><li><a href="??" value="WA">Washington</a></li><li><a href="??" value="WV">West Virginia</a></li><li><a href="??" value="WI">Wisconsin</a></li><li><a href="??" value="WY">Wyoming</a></li></ul>

<input name="compare-savings" value="" type="hidden"></div>
</form>

Open in new window

0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question