我有我的网站的用户的警报消息:防止额外的弹出窗口使用javascript
alert("Message to users");
每一个继承模板页面上会出现此消息。
我想为用户创建一个选项,以阻止使用javascript的附加警报消息。我想到的是消息下方的复选框,以阻止其他消息。
我有我的网站的用户的警报消息:防止额外的弹出窗口使用javascript
alert("Message to users");
每一个继承模板页面上会出现此消息。
我想为用户创建一个选项,以阻止使用javascript的附加警报消息。我想到的是消息下方的复选框,以阻止其他消息。
这里有一个演示:http://jsfiddle.net/jR8hk/
每次运行它,或者重新加载页面时,警报在一秒钟后出现。
但是,如果您选中该复选框,您将再也看不到警报。正如我在评论中所建议的,它通过在本地计算机中设置使用localStorage的首选项来工作。
以下是部分:
HTML:
<div id=alert>
THIS IS A MESSAGE !
<br><br><input type=checkbox id=alert_cb> SHUT UP
<br><input type=button id=alert_close value = close>
</div>
<p>
<br>normal content <br>normal conten <br>normal content <br>normal conten <br>normal content <br>normal conten <br>normal content <br>normal conten <br>normal content <br>normal conten <br>normal content <br>normal conten <br>normal content <br>normal conten <br>normal content <br>normal conten <br>normal content <br>normal conten <br>normal content <br>normal conten <br>normal contenttttttttttt
</p>
CSS:
#alert {
display:none;
background-color: red;
z-index:2;
position: fixed;
left: 40px;
right: 40px;
top: 40px;
height: 100px;
}
的JavaScript:
setTimeout(function(){
if (!localStorage['stop_please']) $('#alert').show('slow');
}, 1000);
$('#alert_close').click(function(){
if ($('#alert_cb').is(':checked')) localStorage['stop_please'] = "pleeeeaaaaase";
$('#alert').hide();
});
如果css不够好,你可能会想调整它。
Firefox在浏览器中内置了此功能。我认为这是你试图复制的功能?
这里有两种不同的问题。你问如何创建警报框,或者如何存储设置?
如果你问如何创建警告框,有几个方法,你可以去它:
选项1:看起来像@dystroy提供了一个如何使用jQuery来做到这一点的例子。
选项2:它会弹出一个确定/取消按钮。您可以将信息设置为一样的东西:
“发送给用户”
你想防止
这些弹出窗口从现在开始?OK 取消
如果他们点击确定,保存设置以某种方式阻止弹出窗口的再次发生。
或者如果您希望用户能够默认点击确定而不阻止它们,您可以将消息更改为“按取消以防止将来出现这些弹出窗口”。
如果您希望此设置在页面加载之间保持不变,您必须将其存储在数据库,cookie,会话变量或其他内容中。
您的代码可能是这个样子:
if (/* check the setting to see if we should display a message */)
{
var r = confirm("Message to users\n\nPress Cancel to prevent these popups in the future");
if (!r)
{
// do something to store this setting
}
}
您必须将他们的偏好存储在某处(可能是应用程序数据库中的用户记录或用户/警报表)。您无法在网页中执行此操作,因为一旦它们刷新或移动到另一页面,它们的设置就会消失。 –
@RobertHarvey为什么不使用localStorage? oladitan,你将不得不做一个自定义对话框(如果你是web开发新手,请看jQuery UI)以添加一个自定义复选框。 –
+1使用localStorage – FilmJ