保持网站主题相同..cookies?
我想我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用所有 javascript 并且没有任何服务器端代码可以使用,这里有一个设置和读取 cookie 的 JS 示例:
将此函数添加到您的 JS,然后在主题更改时运行它:
读取 cookie并在页面加载时设置主题(使用 jQuery 或类似的 $(document).ready() 会比 onload 更好,但这里有一个直接的 js/dom 示例)
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:
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)
您可以使用会话来跟踪服务器端用户的首选项。当用户返回并选择了主题(您可以从会话变量中确定)时,服务器将提供设置了背景图像而不是纯色的 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.