By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,616 Members | 2,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,616 IT Pros & Developers. It's quick & easy.

Set cookie and redirect user to a visited subdomain

P: 3
The cookie is not holding and the domain example.com cannot redirect to a.example.com when I type on the address bar. Anyone good at cookies? Any help will be very much appreciated.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" src="/static/js/jquery-1.3.2.min.js"></script>
  2. <script type="text/javascript">
  3. $(function(){
  4.     var city = readCookie('city');
  5.     if(city !=null && city !=''){
  6.     window.location.href = 'http://' + city + '.example.com';
  7.     }
  8. $('#citygo').change(function(){
  9.     var city = $(this).val();
  10.     window.location.href = 'http://' + city + '.example.com';
  11. });
  12. });
  13.  
  14. function createCookie(name,value,days) {
  15.     if (days) {
  16.         var date = new Date();
  17.         date.setTime(date.getTime()+(days*24*60*60*1000));
  18.         var expires = "; expires="+date.toGMTString();
  19.     }
  20.     else var expires = "";
  21.     document.cookie = name+"="+value+expires+"; path=/";
  22. }
  23. function readCookie(name) {
  24.     var nameEQ = name + "=";
  25.     var ca = document.cookie.split(';');
  26.     for(var i=0;i < ca.length;i++) {
  27.         var c = ca[i];
  28.         while (c.charAt(0)==' ') c = c.substring(1,c.length);
  29.         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  30.     }
  31.     return null;
  32. }
  33. function eraseCookie(name) {
  34.     createCookie(name,"",-1);
  35. }
  36. </script>
  37.  
  38.  
  39. <select id="citygo">
  40.     <option value="0">Select City</option>
  41.     <option value="amsterdam">Amsterdam</option>
  42.     <option value="newyork">New York</option>
  43.     <option value="london">London</option>
  44.     <option value="cardiff">Cardiff</option>
  45. </select>
Dec 3 '13 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
the cookie is not holding because cookies are saved per domain (for security reasons), and since you change the domain
Dec 4 '13 #2

P: 3
Any solution Dormilich? Thanks.
Dec 4 '13 #3

Dormilich
Expert Mod 5K+
P: 8,639
sorry, nothing I know in JS. maybe you could do something via server-side scripting.
Dec 4 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.