?
Solved

Javascript to write cookie for state selector used on another page

Posted on 2011-03-08
4
Medium Priority
?
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 29

Accepted Solution

by:
Pravin Asar earned 1500 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
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…
Suggested Courses

762 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