Javascript 还记得饼干吗
我想制作一个cokkie,它可以在netsoltech.com上保存我选择的区域,每当用户在地图上选择区域时,它可以保存cookie,当下次用户进入域时,自动转到第一次单击区域页面,我制作此代码 但问题是,当用户下次打开第一个区域选择器页面时,它可以刷新并转到区域页面。。不是自动的Javascript 还记得饼干吗,javascript,Javascript,我想制作一个cokkie,它可以在netsoltech.com上保存我选择的区域,每当用户在地图上选择区域时,它可以保存cookie,当下次用户进入域时,自动转到第一次单击区域页面,我制作此代码 但问题是,当用户下次打开第一个区域选择器页面时,它可以刷新并转到区域页面。。不是自动的 <script> /* Cookie script - Scott Andrew Popup script, Copyright 2005, Sandeep Gangadharan */
<script>
/*
Cookie script - Scott Andrew
Popup script, Copyright 2005, Sandeep Gangadharan
*/
function newCookie(name,value,days) {
var days = 10; // the number at the left reflects the number of days for the cookie to last
// modify it according to your needs
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 nameSG = name + "=";
var nuller = '';
if (document.cookie.indexOf(nameSG) == -1)
return nuller;
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(nameSG) == 0) return c.substring(nameSG.length,c.length); }
return null; }
function eraseCookie(name) {
newCookie(name,"",1); }
function toMem(region) {
newCookie('region', region);
window.location= "http://www.netsoltech.com/"+region+"/index";
}
function remCookie() {
window.location= "http://www.netsoltech.com/"+readCookie("region")+"/index";
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
remCookie();
});
</script>
/*
Cookie脚本-Scott Andrew
弹出脚本,版权2005,Sandeep Gangadharan
*/
函数newCookie(名称、值、天数){
var days=10;//左侧的数字反映cookie持续的天数
//根据您的需要进行修改
如果(天){
变量日期=新日期();
date.setTime(date.getTime()+(天*24*60*60*1000));
var expires=“;expires=“+date.togmString();}
else var expires=“”;
document.cookie=name+“=”+value+expires+“path=/”;}
函数readCookie(名称){
var nameSG=name+“=”;
var nuller='';
if(document.cookie.indexOf(nameSG)=-1)
返回零位器;
var ca=document.cookie.split(“;”);
对于(var i=0;i试试这个
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
//redirect here
}
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
function checkCookie() {
var region = getCookie("region");
if (region != null && region != "") {
alert("redirect to " + region); //replace this with redirect
}
}
checkCookie(); //check this on page load
函数setCookie(c_名称、值、exdays){
var exdate=新日期();
exdate.setDate(exdate.getDate()+exdays);
var c_value=escape(value)+(exdays==null)?“”:“expires=“+exdate.toutString());
document.cookie=c_name+“=”+c_值;
//重定向到这里
}
函数getCookie(c_名称){
变量i,x,y,ARRcookies=document.cookie.split(“;”);
对于(i=0;i<0.length;i++){
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf(“=”);
y=ARRcookies[i].substr(ARRcookies[i].indexOf(“=”)+1);
x=x.replace(/^\s+|\s+$/g,”);
如果(x==c_名称){
返回unescape(y);
}
}
}
函数checkCookie(){
var region=getCookie(“区域”);
如果(region!=null&®ion!=“”){
警报(“重定向到”+区域);//将其替换为重定向
}
}
checkCookie();//在页面加载时检查此项
以及HTML
<a href="#" onclick="setCookie('region', 'europe', 365); return false;">europe</a>
JSFiddle