homepage Welcome to WebmasterWorld Guest from 184.73.40.21
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Remember cookie and redirect to a visited subdomain
iseven



 
Msg#: 4627817 posted 4:19 pm on Dec 4, 2013 (gmt 0)

The code is working but the cookie is not holding and the domain example.com cannot redirect to a.example.com when I type on the address bar. The issue should be the cookies are saved per domain for security reasons. I need to set cookies to remember and redirect to a visited subdomain. Can anyone help? Thanks.

<script type="text/javascript" src="/static/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(function(){
var city = readCookie('city');
if(city !=null && city !=''){
window.location.href = 'http://' + city + '.example.com.com';
}
$('#citygo').change(function(){
var city = $(this).val();
window.location.href = 'http://' + city + '.example.com.com';
});
});

function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
</script>


<select id="citygo">
<option value="0">Select City</option>
<option value="amsterdam">Amsterdam</option>
<option value="newyork">New York</option>
<option value="london">London</option>
<option value="cardiff">Cardiff</option>
</select>

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4627817 posted 9:11 pm on Dec 4, 2013 (gmt 0)

the cookies are saved per domain for security reasons. I need to set cookies to remember and redirect to a visited subdomain

Saved "per domain" or "per subdomain"? They're crucially different things. Do you need one subdomain to read a different subdomain's cookies, or do you need the overall domain to read a subdomain's cookies?

iseven



 
Msg#: 4627817 posted 9:19 pm on Dec 4, 2013 (gmt 0)

One domain redirects to all sub-domain.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4627817 posted 10:02 pm on Dec 4, 2013 (gmt 0)

I think you'd have to approach it from the other direction. Instead of letting the subdomains set their own cookies, set all cookies on the domain as a whole, and let the name and/or value of the cookie carry the subdomain information.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved