Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Javascript to write cookie for state selector used on another page

Posted on 2011-03-08
4
Medium Priority
?
296 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

636 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