简体   繁体   English

如何制作一个可以被多个页面访问的javascript变量?

[英]How to make a javascript variable that is accessible by multiple pages?

I am trying to make my own mini web app and would like to implement a dark mode.我正在尝试制作自己的迷你网络应用程序,并希望实现暗模式。 I already have the code for the dark mode itself, but, when I change pages the dark mode doesn't stay.我已经有了暗模式本身的代码,但是,当我更改页面时,暗模式不会保留。 Is there such a thing as a 'page-wide' variable?是否有“页面范围”变量之类的东西? I have already looked around and found things like local storage and session storage , but, to my knowledge, those are just for a specific page, and not for an entire website.我已经环顾四周,发现了诸如local storagesession storage类的东西,但是,据我所知,这些仅适用于特定页面,而不适用于整个网站。

(would prefer if the solution used pure js) (如果解决方案使用纯 js 会更喜欢)

As far as I know, the localStorage has no expiration date, meaning that you can use it as long as the user explicitly tells the browser to clear its data (correct me if I'm wrong).据我所知, localStorage没有到期日期,这意味着您可以使用它,只要用户明确告诉浏览器清除其数据(如果我错了,请纠正我)。

I'm maybe not that good at explaining, so here's a link to how to use it: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage我可能不太擅长解释,所以这里有一个如何使用它的链接: https : //developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

FYI, you can use @media (prefers-color-scheme: dark) in CSS and in future browsers the color scheme will be determined by the browser settings.仅供参考,您可以在 CSS 中使用@media (prefers-color-scheme: dark) ,在未来的浏览器中,配色方案将由浏览器设置决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM