2016-02-07 19 views
0

我正在尝试设置Cookie或会话变量以使网站上的用户首选项保留在站点的每个页面上,并且我正在尝试使用它Javascript,但我愿意尝试任何有效的代码。记录整个站点的用户首选项的Cookie或会话变量

我想让我的onclick用户选项在网站的每个页面上为其会话保持活动状态。因此,如果他们导航到第2页,他们从页面1的布局选择被记住并且在页面2上保持活跃。

我正在建设的基本上是一个网站上的电子阅读器,所以我希望用户有能力更改字体大小,更改背景颜色并更改文字宽度以适合他们的偏好。我已经掌握了所有这些工作,但是当他们点击下一页时,设置清晰了,他们必须重新设置它们。我希望网站记住他们在网站上的整个会话的选择或者一段时间,即使这是可能的。

我对Javascript很陌生(这是我写的第一天),所以下面的代码可能会非常糟糕。

我不知道我是否需要使用cookie或会话变量,但是我发现的所有修复都不起作用,或者它们并非真正用于此目的,所以我可以'不要使用它们。

在我所创作的“故事”里面的div改变一切,当在上面的选项,用户点击,和选项的代码:

  • 放大字体
  • 缩小字体
  • 黑暗主题
  • 轻主题
  • 棕褐色主题
  • 全宽
  • 75%宽度
  • 50%宽度

这一切工作,到目前为止,但我不能让它坚持与用户,如果他们导航到另一个页面。当他们点击下一页时,他们的偏好就清楚了,那就是问题所在。

这是我现在的代码。

的HTML:

<html> 
<head> 
    <title>Test</title> 
    <script type="text/javascript" src="cookies.js"></script> 
    <script type="text/javascript" src="customize.js"></script> 

<style> 
.text-tools{margin:auto; text-align:center;} 
</style> 
</head> 
<body> 
<div class="text-tools"> 
<img id="plustext" alt="Increase text size" src="images/A-plus.png" onclick="resizeText(1)" style="width:25px;" /> 
<img id="minustext" alt="Decrease text size" src="images/A-minus.png" onclick="resizeText(-1)" style="width:25px;" /> 
<a href="#" onclick="dark()"><img src="images/dark.png" style="width:25px;" /></a> 
<a href="#" onclick="light()"><img src="images/light.png" style="width:25px;" /></a> 
<a href="#" onclick="sepia()"><img src="images/sepia.png" style="width:25px;" /></a> 
<a href="#" onclick="full()"><img src="images/full.png" style="width:25px;" /></a> 
<a href="#" onclick="third()"><img src="images/third.png" style="width:25px;" /></a> 
<a href="#" onclick="half()"><img src="images/half.png" style="width:25px;" /></a> 
</div> 
<div id="story"> 
<div id="sfont"> 
<div id="sbg"> 
<div id="stext"> 
<h1 id="chtitle">The Title</h1> 
<p style="font-size:1em;font-color:#000;"><span id="cap1" style="font-size:2em;">S</span>ome text goes here...</p> 
<a href="page2.html">Page Two</a> 
</div> 
</div> 
</div> 
</div> 
<p>Other stuff here</p> 

</body> 
</html> 

自定义的Javascript:

// Change font size 
function resizeText(multiplier) { 
    if (document.getElementById("sfont").style.fontSize == "") { 
    document.getElementById("sfont").style.fontSize = "1.0em"; 
    } 
    document.getElementById("sfont").style.fontSize = parseFloat(document.getElementById("sfont").style.fontSize) + (multiplier * 0.2) + "em"; 
} 

//Change background color 
<!--// 

function dark() 

{ 
document.getElementById("sbg").style.backgroundColor="#474747" 
document.getElementById("stext").style.color="#cdcdcd" 
document.getElementById("chtitle").style.color="#e5e5e5" 
document.getElementById("cap1").style.color="#e5e5e5" 
} 

function light() 

{ 
document.getElementById("sbg").style.backgroundColor="#ffffff" 
document.getElementById("stext").style.color="#8C9398" 
document.getElementById("chtitle").style.color="#686868" 
document.getElementById("cap1").style.color="#686868" 
} 

function sepia() 

{ 
document.getElementById("sbg").style.backgroundColor="#c1b29b" 
document.getElementById("stext").style.color="#77674f" 
document.getElementById("chtitle").style.color="#564a36" 
document.getElementById("cap1").style.color="#564a36" 

} 

//Change div width 
function full() 

{ 
document.getElementById('story').setAttribute("style","width:100%;margin:auto"); 
} 

function third() 

{ 
document.getElementById('story').setAttribute("style","width:75%;margin:auto"); 
} 

function half() 

{ 
document.getElementById('story').setAttribute("style","width:50%;margin:auto"); 
} 

//--> 

这是饼干的Javascript不起作用:

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); 
} 

我敢肯定,我没有正确使用cookies.js,但我无法弄清楚如何使它工作。

修复cookies.js的任何帮助,以便它的工作或替代方法将非常有用。

在此先感谢任何想解决这个问题的人。

回答

0

我试用了你的Cookies Javascript,它工作正常。
如果您希望用户选项在网站的每个页面上保持活动状态,您可能需要考虑使用sessionStorage

尽管存在语法错误,但您的自定义JavaScript仍在工作。

我的建议是保存用户选项,因为他们正在设置它们,然后在每个加载的页面中读取它们。

这里是您需要的自定义Javascript。我也纠正了语法。我会使用sessionStorage来处理这种情况,因为这种方式你不需要cookies。

window.onload = loadSettings; 
// Change font size 
function resizeText(multiplier) { 
    sessionStorage.setItem("fontSize", multiplier); 
    if (document.getElementById("sfont").style.fontSize == "") { 
     document.getElementById("sfont").style.fontSize = "1.0em"; 
    } 
    document.getElementById("sfont").style.fontSize = parseFloat(document.getElementById("sfont").style.fontSize) + (multiplier * 0.2) + "em"; 
} 

function dark() { 
    sessionStorage.setItem("background", "dark"); 
    document.getElementById("sbg").style.backgroundColor = "#474747"; 
    document.getElementById("stext").style.color = "#cdcdcd"; 
    document.getElementById("chtitle").style.color = "#e5e5e5"; 
    document.getElementById("cap1").style.color = "#e5e5e5"; 
} 

function light() { 
    sessionStorage.setItem("background", "light"); 
    document.getElementById("sbg").style.backgroundColor = "#ffffff"; 
    document.getElementById("stext").style.color = "#8C9398"; 
    document.getElementById("chtitle").style.color = "#686868"; 
    document.getElementById("cap1").style.color = "#686868"; 
} 

function sepia() { 
    sessionStorage.setItem("background", "sepia"); 
    document.getElementById("sbg").style.backgroundColor = "#c1b29b"; 
    document.getElementById("stext").style.color = "#77674f"; 
    document.getElementById("chtitle").style.color = "#564a36"; 
    document.getElementById("cap1").style.color = "#564a36"; 
} 

//Change div width 
function full() { 
    sessionStorage.setItem("divWidth", "full"); 
    document.getElementById('story').setAttribute("style", "width:100%;margin:auto"); 
} 

function third() { 
    sessionStorage.setItem("divWidth", "third"); 
    document.getElementById('story').setAttribute("style", "width:75%;margin:auto"); 
} 

function half() { 
    sessionStorage.setItem("divWidth", "half"); 
    document.getElementById('story').setAttribute("style", "width:50%;margin:auto"); 
} 

function loadSettings() { 
    var fontSize = sessionStorage.getItem("fontSize") && parseFloat(sessionStorage.getItem("fontSize")); 
    var background = sessionStorage.getItem("background") && sessionStorage.getItem("background"); 
    var divWidth = sessionStorage.getItem("divWidth") && sessionStorage.getItem("divWidth"); 
    resizeText(fontSize); 
    if (background == "dark") dark(); 
    else if (background == "light") light(); 
    else if (background == "sepia") sepia(); 
    if (divWidth == "full") full(); 
    else if (divWidth == "third") third(); 
    else if (divWidth == "half") half(); 
} 
+0

非常感谢!这工作完美。 – CC87

0

我会使用localStorage保存信息。每次发出HTTP请求时,Cookie都会将信息发送到服务器,例如获取新页面。而且,他们不会节省更多的信息。 w3schools has a page on localStorage and sessionStorage这可能值得一看。

为了尽可能少地修改你的代码,我建议你定义一个新的函数来存储它调用的函数然后调用它们。

function update(key, funct) { 
    localStorage.setItem(key, funct); 
    window[funct].call(); 
} 

你需要改变你的链接:

<a href="#" onclick="update('color','dark')">... 
<a href="#" onclick="update('color','light')">... 
<a href="#" onclick="update('color','sepia')">... 
<a href="#" onclick="update('size','full')">... 
<a href="#" onclick="update('size','third')">... 
<a href="#" onclick="update('size','half')">... 

当加载一个新的页面,您可以检查给定的设置已改变,并调用函数来执行给定的变化。

if (funct = localStorage.getItem('color')) { 
    window[funct].call(); 
} 
if (funct = localStorage.getItem('size')) { 
    window[funct].call(); 
} 

最好是什么虽然是这样做在一个循环:

var functs = ['color', 'size']; 
for (var i=0; i < functs.length; i++) { 
    if (funct = localStorage.getItem(functs[i])) { 
     window[funct].call(); 
    } 
} 

要查看工作示例下面的代码保存为一个html文件,你的网络服务器中,你的Web浏览器中加载它:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <script> 

    // Define fuctions: dark, light, etc. so that they just print their names 
    var fs = ['dark', 'light', 'sepia', 'full', 'third', 'half'] 
    for (var i=0; i < fs.length; i++) { 
     var f = fs[i]; 
     window[f] = (function(funct) { 
      return function() { 
       alert(funct); 
      } 
     })(f) 
    } 

    // Update localStorage and call function 
    function update(key, funct) { 
     localStorage.setItem(key, funct); 
     window[funct].call(); 
    } 

    // Page has loaded 
    document.addEventListener("DOMContentLoaded", function() { 

     // Call functions when page is loaded 
     var functs = ['color', 'size']; 
     for (var i=0; i < functs.length; i++) { 
      if (funct = localStorage.getItem(functs[i])) { 
       window[funct].call(); 
      } 
     } 
    }); 
    </script> 
</head> 
<body> 
    <a href="#" onclick="update('color','dark')">dark</a> 
    <a href="#" onclick="update('color','light')">light</a> 
    <a href="#" onclick="update('color','sepia')">sepia</a><br> 
    <a href="#" onclick="update('size','full')">full</a> 
    <a href="#" onclick="update('size','third')">third</a> 
    <a href="#" onclick="update('size','half')">half</a> 
</body> 
</html> 

如果您使用调试控制台,我会建议在代码改变alertconsole.log。它不那么干扰。

+0

谢谢你在这方面的工作。我回答了第一个答案,但我还在学习,所以这个答案可能会帮助我学习更多。 – CC87