获取cookie值以在页面更改时加载

时间:2013-05-24 08:36:42

标签: javascript cookies

您好我有这个脚本:http://flamencopeko.net/styleswitch.js,它链接到我网站http://flamencopeko.net上的所有网页。它工作正常,但更改页面时不应用存储的值。我相信功能和cookie本身都很好。偏好不会直观地粘在上面。

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 increaseTableSize() {
   var t = document.getElementsByTagName('table');
   for(i=0;i<t.length;i++) {
       t[i].style.width = 1000+"px"
   }
}
function decreaseTableSize() {
   var t = document.getElementsByTagName('table');
   for(i=0;i<t.length;i++) {
      t[i].style.width = 677+"px"
   }
}
function setTableSize() {
   var t = document.getElementsByTagName('table');
   for(i=0;i<t.length;i++) {
      t[i].style.width = selectedwidth+"px"
   }
}

function increaseFontSize() {
   var p = document.getElementsByTagName('td');
   for(i=0;i<p.length;i++) {
       p[i].style.fontSize = 16+"px"
   }
}
function decreaseFontSize() {
   var p = document.getElementsByTagName('td');
   for(i=0;i<p.length;i++) {
      p[i].style.fontSize = 10+"px"
   }
}
function setFontSize() {
   var t = document.getElementsByTagName('td');
   for(i=0;i<t.length;i++) {
      t[i].style.width = selectedsize+"px"
   }
}

var selectedwidth=readCookie("peko_table")
if (document.getElementByTagName && selectedwidth!=null) //load user chosen width from cookie if there is one stored
setTableSize()

var selectedsize=readCookie("peko_font")
if (document.getElementByTagName && selectedsize!=null) //load user chosen font from cookie if there is one stored
setFontSize()

这是toc.php中四个按钮的代码:

  <a href="javascript:createCookie('peko_table','600',50); decreaseTableSize();"><img src="/ico/standard.gif" width="12" height="12" hspace="1" vspace="1" />slim</a> / 
  <a href="javascript:createCookie('peko_table','1000',50); increaseTableSize();"><img src="/ico/wide.gif" width="12" height="12" hspace="1" vspace="1" />wide</a> / 
  <a href="javascript:createCookie('peko_font','10',50); decreaseFontSize();"><img src="/ico/minus.gif" width="12" height="12" hspace="1" vspace="1" />small text</a> / 
  <a href="javascript:createCookie('peko_font','18',50); increaseFontSize();"><img src="/ico/plus.gif" width="12" height="12" hspace="1" vspace="1" />large text</a>

1 个答案:

答案 0 :(得分:0)

您甚至在解析页面之前运行JavaScript。

将styleswitch.js的JavaScript include从头部移动到关闭的body标记之前。