[英]How to popup modal on page load
I'm learning on how to popup modal when a page load.我正在学习如何在页面加载时弹出模式。 For example when I click on a button in home.html that is linked to comment.html, when comment.html load then modal popup.
例如,当我单击 home.html 中链接到 comment.html 的按钮时,当 comment.html 加载然后模态弹出窗口时。 But if I navigate to comment.html without clicking on the button then modal will not popup.
但是,如果我导航到 comment.html 而不单击按钮,则不会弹出模式。 Let modal only popup when I click on the button.
仅当我单击按钮时才弹出模式。
home.html主页.html
<!-- Another button that linked to comment without mod data-target -->
# I do not want Modal to popup in comment.html when I click on this button
<a href="{% url 'site:comment' %}">Comment</a>
<!-- Button trigger modal -->
# I want modal to popup in comment.html when I click on this button
<a href="{% url 'site:comment' %}" data-toggle="modal" data-target="#basicExampleModal">
Launch Modal in Comment
</a>
comment.html评论.html
<!-- Modal -->
<div class="modal fade" id="basicExampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
$(document).ready(function(){
if(localStorage.getItem('popState') != 'shown'){
$("#basicExampleModal").delay(2000).fadeIn();
localStorage.setItem('popState','shown')
}
$('#basicExampleModal').modal('show');
});
The above code will create a popup Model automatically.上面的代码将自动创建一个弹出窗口 Model。 Hope you find it useful.
希望你觉得它有用。
In short:简而言之:
in home templates file在主页模板文件中
<!-- If modal should not be shown -->
<a href="{% url 'site:comment' %}">Comment</a>
<!-- If modal should be shown : create local storage item on clicking the a tag -->
<a href="{% url 'site:comment' %}" onclick="localStorage.setItem('showModalInCommentPage', '1')">Launch Modal in Comment</a>
in comments template file在评论模板文件中
<script>
$(document).ready( function() {
var showmodal = localStorage.getItem('showModalInCommentPage');
if (showmodal == '1') {
$('#basicExampleModal').modal('show');
localStorage.removeItem('showModalInCommentPage');
}
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.