简体   繁体   English

如何让浏览器记住用户选择的内容?

[英]How to make the browser remember what the user choose?

I have this code. 我有这个代码。 I don't really know anything about Javascript so I pretty much found it on the internet. 我对Javascript一无所知,所以我几乎在互联网上找到它。 What it does basically is closes the big header image at the top of the page. 它的作用基本上是关闭页面顶部的大标题图像。 I want to know how do I make it so that the browser remembers what the user has chosen? 我想知道如何制作它以便浏览器记住用户选择的内容? FOr example if the person clicks to hide it and then you reload the page, it should remain hidden and not default to the original. 例如,如果此人单击以隐藏它然后您重新加载页面,它应该保持隐藏状态而不是默认为原始页面。

<script language='javascript'>
function toggle() {
var pagehead = document.getElementById(&quot;HTML6&quot;);
var xbutton = document.getElementById(&quot;hide-header&quot;);
if(pagehead.style.display == &quot;none&quot;) {
pagehead.style.display = &quot;block&quot;;
xbutton.innerHTML = &quot;<img border='0' src='https://lh5.googleusercontent.com/-S5r38GtSF6s/Ui04r4eS0yI/AAAAAAAADpQ/qRnrSX2MpcY/w16-h15-no/close+X.png'/>&quot;;
}
else {
pagehead.style.display = &quot;none&quot;;
xbutton.innerHTML = &quot;<img border='0' src='https://lh5.googleusercontent.com/-S5r38GtSF6s/Ui04r4eS0yI/AAAAAAAADpQ/qRnrSX2MpcY/w16-h15-no/close+X.png'/>&quot;;
}
} 
</script>

Not sure if it's even possible. 不确定它是否可能。 I have no idea really. 我真的不知道。 Here is my website for reference: linkvier.com I really hope I can get this code to work since I plan on using it on ads and stuff as well. 这是我的网站供参考: linkvier.com我真的希望我可以让这个代码工作,因为我打算在广告和东西上使用它。

You need to use the browser cookies. 您需要使用浏览器cookie。 Take a look at jQuery.cookie . 看看jQuery.cookie There are examples there. 那里有例子。

If you don't have to support old browsers then check out html's 5 localStorage it's pretty awesome. 如果您不必支持旧浏览器,那么请查看html的5 localStorage它非常棒。 :-) :-)

For older browsers use cookies or one of those scripts that emulate localStorage using cookies. 对于较旧的浏览器,使用cookie或其中一个使用cookie模拟localStorage的脚本

as far as i know http is stateless you can use cookies to get this Read Stateless_protocol 据我所知,http是无状态的,你可以使用cookie来获得这个Read Stateless_protocol

look at checkCookie() getCookie(c_name) and setCookie(c_name,value,exdays) here @ 在这里查看checkCookie()getCookie(c_name)和setCookie(c_name,value,exdays)@
w3schools Cookie Examples w3schools Cookie示例

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

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