Solved

Javascript to write cookie for state selector used on another page

Posted on 2011-03-08
4
288 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now