保持网站主题相同..cookies?

发布于 2024-08-08 23:45:57 字数 334 浏览 4 评论 0原文

我想我需要使用 javascript 来做到这一点,所以我把它放在堆栈溢出上。所以,我的网站有两个主题,唯一的区别是一个是纯色(背景),另一个是重复的图像,所以我没有为它们制作单独的 css 文件。我在导航栏中有两个链接可以更改它(使用javascript)。在 css 文件中它是纯色的,所以当页面加载时它就是这样开始的。当单击图像主题链接时,它将 document.body.backgroundImage 设置为图像,而当按下纯色主题链接时,它只是将背景图像设置为“”(空),以便您可以再次看到颜色。那么我怎样才能使主题持久,当用户转到另一个页面以及他们再次返回时不改变。谢谢。

编辑:我可以使用 PHP 或 javascript。

I think I will need to use javascript to do this so I put it here on stack overflow. So, I have two themes for my website, the only difference is that one is a solid colour (background), the other a repeated image, so I didn't make separate css files for them. I have two links in the navigation bar that change it (with javascript). In the css file its the solid colour, so when ever the page loads it starts out as that. When the image theme link is clicked, it sets the document.body.backgroundImage to the image, and when the solid colour theme link is pressed it just sets the background image to "" (empty), so that you can see the colour again. So how can I make the theme persistent, not changing when ever the user goes to another page, as well as when they return another time. Thanks.

EDIT: I can use either PHP or javascript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢 2024-08-15 23:45:57

如果您使用所有 javascript 并且没有任何服务器端代码可以使用,这里有一个设置和读取 cookie 的 JS 示例:

将此函数添加到您的 JS,然后在主题更改时运行它:

function set_theme(name){
  document.cookie='sel_theme='+name+';';
  }//so, run set_theme('whatevername'); when it is set by the user

读取 cookie并在页面加载时设置主题(使用 jQuery 或类似的 $(document).ready() 会比 onload 更好,但这里有一个直接的 js/dom 示例)

 window.onload=function(){
var cookie_pos=document.cookie.indexOf('sel_theme=');//locate value in cookie
if(cookie_pos!=-1){//if string was found
  var cookie_flavor=substr(cookie_pos+10,document.cookie.indexOf(';',cookie_pos));//extract the value of the cookie
  /*then run your already existing change theme function using the extracted name*/
  }    

If you're using all javascript and don't have any serverside code to work with, here's a JS example to set and read a cookie:

Add this function to your JS, then run it when the theme is changed:

function set_theme(name){
  document.cookie='sel_theme='+name+';';
  }//so, run set_theme('whatevername'); when it is set by the user

To read the cookie and set the theme on page load (using jQuery or similar $(document).ready() would be better than onload, though, but here's a straight js/dom example)

 window.onload=function(){
var cookie_pos=document.cookie.indexOf('sel_theme=');//locate value in cookie
if(cookie_pos!=-1){//if string was found
  var cookie_flavor=substr(cookie_pos+10,document.cookie.indexOf(';',cookie_pos));//extract the value of the cookie
  /*then run your already existing change theme function using the extracted name*/
  }    
妞丶爷亲个 2024-08-15 23:45:57

您可以使用会话来跟踪服务器端用户的首选项。当用户返回并选择了主题(您可以从会话变量中确定)时,服务器将提供设置了背景图像而不是纯色的 HTML。当用户切换首选项时,您可以发送更新会话变量的 ajax 请求。

You can use a session to keep track of the user's preferences on the serverside. When the user comes back on and has a theme selected (which you can determine from the session variables) then the server will deliver HTML that has backgroundImage set instead of a solid color. When the user toggles preferences you can send out an ajax request that updates the session variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文