简体   繁体   English

显示 Bootstrap Modal 首次页面加载

[英]Display Bootstrap Modal First time page loads

I'm trying to display a bootstrap modal when a person first visits a page (after logging into my app).当一个人第一次访问一个页面时(登录我的应用程序后),我试图显示一个引导模式。 I want to use the modal to offer a "Getting Started" video and also offer a "don't show this to me a again" checkbox so a visitor can bypass the "getting start modal" on future logins.我想使用模态提供“入门”视频,并提供“不再向我显示此内容”复选框,以便访问者可以在以后登录时绕过“入门模态”。

I was able to get the modal to display on page load with the following tutorial:通过以下教程,我能够在页面加载时显示模态:

Launch Bootstrap Modal on page load 在页面加载时启动 Bootstrap Modal

But now I'm stuck on how to get it display only the first time a user visits the page after logging in. (it currently launches on page refreshes etc)但现在我被困在如何让它在用户登录后第一次访问页面时显示。(它当前在页面刷新等时启动)

I'm new to javascript and programming and have created my app with Django Python 2.7.4我是 javascript 和编程的新手,并使用 Django Python 2.7.4 创建了我的应用程序

I really appreciate the time and expertise.我非常感谢时间和专业知识。

To do this, you'd need to use a means to store data about the user that persists between website visits, which is generally referred to as a session .为此,您需要使用一种方法来存储有关在网站访问之间持续存在的用户数据,这通常称为session In this particular case, it sounds like the users might not be logged in, which is referred to more specifically as an anonymous session .在这种特殊情况下,听起来用户可能没有登录,这更具体地称为anonymous session

There are a number of options available to you for storing anonymous sessions.有许多选项可用于存储匿名会话。 The quickest and easiest would be using cookies , but other options include file-based sessions, Html5 storage, or databases.最快和最简单的方法是使用cookie ,但其他选项包括基于文件的会话、Html5 存储或数据库。

From what you've written, I'd say that cookies are probably the best solution for you.根据您所写的内容,我想说 cookie 可能是最适合您的解决方案。

And, as you might expect, Django has built in functionality to ease working with cookies.而且,如您所料,Django 内置了简化 cookie 的功能。 I suggest looking into the documentation on Django sessions , which I find to be really accessible and easy to learn from, to see how to implement this.我建议查看关于 Django sessions 的文档,我发现它非常易于访问且易于学习,以了解如何实现这一点。

If you have any more specific questions about using cookies (or anonymous sessions) in Django, just let me know and I'll try to help.如果您对在 Django 中使用 cookie(或匿名会话)有任何更具体的问题,请告诉我,我会尽力提供帮助。

You can use jquery.cookies.js in combination with modal.js to load the page only once then set an expiry cookie.您可以将 jquery.cookies.js 与 modal.js 结合使用,只加载一次页面,然后设置一个过期 cookie。

            <script src="/path/to/jquery.cookie.js"></script>
            <script>
            $(document).ready(function() {
                 if ($.cookie(‘pop’) == null) {
                     $(‘#myModal’).modal(‘show’);
                     $.cookie(‘pop’, ’7');
                 }
             });
            </script>

You can find more details in this tutorial:您可以在本教程中找到更多详细信息:

http://calebserna.com/bootstrap-modal-email-subscription-form/ http://calebserna.com/bootstrap-modal-email-subscription-form/

Show Bootstrap Modal first time page load首次加载页面时显示 Bootstrap Modal

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.0/jquery.cookie.min.js">
</script>
<script type="text/javascript">
 $(document).ready(function() {
     if ($.cookie('pop') == null) {
         $('#myModal').modal('show');
         $.cookie('pop', '1');
     }
 });
</script>

You can set number of days to hide modal您可以设置隐藏模态的天数

好吧,您可以使用localstorage来存储发生的操作,因此它是持久的,或者您可以使用一个名为“ showncookie并在开始时将其设置为 false,在显示模态之后,您只需将其设置为 true。

There would certainly be ways to do this with Django.肯定有办法用 Django 做到这一点。 You could store the user's IP, for example, and track whether or not a user at that IP has ever hit the "don't show this again" button.例如,您可以存储用户的 IP,并跟踪使用该 IP 的用户是否曾经点击过“不再显示”按钮。 This would be the only real way to know for sure that you weren't showing the video to someone who had requested not to see it.这将是确定您没有向要求不观看视频的人展示视频的唯一真正方法。 On the front end, users can always clear out their own cookies or local storage so front end tracking can get lost.在前端,用户总是可以清除自己的 cookie 或本地存储,这样前端跟踪就可能会丢失。

That said, here's an actual example, since you mentioned being new to JavaScript:也就是说,这是一个实际示例,因为您提到是 JavaScript 新手:

// When the user clicks the button...
localStorage.setItem('no_display', 'true');

// When a user visits the page...
var no_display = localStorage.getItem('no_display');
if (no_display !== 'true') {
  display_the_modal();
}

You can use this Script, I hope this will work:你可以使用这个脚本,我希望这会奏效:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> 
</script>
  
<script type="text/javascript">
 $(document).ready(function() {
     if (sessionStorage.getItem('#myModal') !== 'true') {
         $('#myModal').modal('show');
         sessionStorage.setItem('#myModal','true');     
     }
 });
</script>

You can try this runnable code-你可以试试这个可运行的代码——

 $(document).ready(function() { if ($.cookie('pop') == null) { $('#myModal').modal('show'); $.cookie('pop', '7'); } });
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <div class="container"> <h2>Modal Example</h2> <!-- Trigger the modal with a button --> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> <!-- Modal --> <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Modal Header</h4> </div> <div class="modal-body"> <p>Some text in the modal.</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> </div>

More can be found here .这里可以找到更多信息

If u approach this way, u will not get your problem (works for me).如果你以这种方式接近,你就不会遇到你的问题(对我有用)。

So, what is done is status is saved in cookey.因此,所做的是将状态保存在 cookie 中。 Depending on cookey, it is decided if the pop up need to be shown in the first time or not.根据cookie,决定是否需要在第一次显示弹出窗口。

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

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