jQuery Cookie 问题
我环顾四周,似乎找不到任何对我来说足够简单的东西...
我设置了一个网页来检测当前正在运行的浏览器,以及它是否是 Firefox 4.0 以外的浏览器,它会显示一个隐藏的 div,并发出警告,指出该页面最好在 Firefox 4.0 或更高版本中查看。该 div 中有一个按钮,可隐藏 div onclick。
我正在寻找一种方法来记住在会话期间已单击此按钮,以便当用户单击我的“主页”页面时,他们不会每次都收到相同的消息。
当前代码:
<head>
<script src="js/browsercheck.js"></script>
<script type="text/javascript">
// external script "browsercheck.js" checks
// which browser/version is being used
// check browser and display message if != Firefox 4.0 or >
function checkBrowser() {
var browser = BrowserDetect.browser;
var version = BrowserDetect.version;
if (browser == "Firefox") {
if (version >= 4) {
// do nothing
}
} else {
document.getElementById("coverall").style.visibility="visible";
document.getElementById("browser").innerHTML = browser + " " + version;
}
}
// remove overlay if user commands
function removeCoverall() {
document.getElementById("coverall").style.visibility="hidden";
}
</script>
</head>
<body>
<div id="coverall" style="visibility:hidden;">
<p>I see you're using <span id="browser"></span>. Please use Firefox.</p>
<button type="button" onclick="removeCoverall()">I understand</button>
</div>
</body>
I've looked around a lot, and can't seem to find anything that's simple enough for me to do...
I've set up a web page that detects which browser is currently running, and if it's something other than Firefox 4.0, it displays a hidden div that gives a warning stating that the page is best viewed in Firefox 4.0 or greater. Within that div is a button that hides the div onclick.
I'm looking for a way to remember that this button has been clicked during a session, so that when a user clicks on my "home" page, they don't get the same message every time.
Current code:
<head>
<script src="js/browsercheck.js"></script>
<script type="text/javascript">
// external script "browsercheck.js" checks
// which browser/version is being used
// check browser and display message if != Firefox 4.0 or >
function checkBrowser() {
var browser = BrowserDetect.browser;
var version = BrowserDetect.version;
if (browser == "Firefox") {
if (version >= 4) {
// do nothing
}
} else {
document.getElementById("coverall").style.visibility="visible";
document.getElementById("browser").innerHTML = browser + " " + version;
}
}
// remove overlay if user commands
function removeCoverall() {
document.getElementById("coverall").style.visibility="hidden";
}
</script>
</head>
<body>
<div id="coverall" style="visibility:hidden;">
<p>I see you're using <span id="browser"></span>. Please use Firefox.</p>
<button type="button" onclick="removeCoverall()">I understand</button>
</div>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 jQuery 和 cookie 插件,您可以执行以下操作:
更多详细信息,请访问:http://www.electrictoolbox.com/jquery-cookies/
Using jQuery and the cookie plugin, you can do:
More details at: http://www.electrictoolbox.com/jquery-cookies/
在
removeCoverall
函数中,您可以设置一个 cookie 来指示用户关闭 div 并在checkBrowser
函数中验证 cookie 是否存在,然后再显示 div。In the
removeCoverall
function you could set a cookie which indicates that the user closed the div and incheckBrowser
function verify if the cookie is present before showing the div.JS
现在在 JavaScript 中检索 cookie 可能很困难,但您可以使用 PHP!
PHP
如果用户拥有 cookie,上面的代码会注入一个 return 语句,因此对该函数的调用不会执行任何操作。
正如其他帖子中提到的,我强烈建议您使用 jQuery 来满足您的所有 javascript 需求。它非常流行、稳定且有用!我只给出了我的答案,因为它不使用任何第三方解决方案。
JS
Now retrieving the cookie can be difficult in javascript, but you can use PHP!
PHP
The above code injects a return statement if the user has the cookie, so the call to the function won't do anything.
As mentioned in the other posts, I highly recommend you use jQuery for all your javascript needs. It's very popular, stable and useful! I only gave my answer as it does not use any third party solution.