Bootstrap Modal Window pop-up after 5sec - javascript

Do anyone know how to make bootstrap modal window pop-up after 5sec ?
I had done a Modal Window with auto pop up but I wish it's pop up delay 5 sec .
can anyone help ?
thanks for help .
here my code .
<div class="modal fade in" id="formSaludo" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: block;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5><strong>給我們一個小小的贊吧~我們會帶給你更多的趣事,新聞,最新動態!您的一個贊給我們帶來無限的力量!</strong></h5>
<img class="img-responsive center-block " src="funnylike.jpg">
</div>
<div class="modal-body">
<div class="fb-like" data-href="https://www.facebook.com/cloudsblacks" data-width="200" data-layout="button_count" data-action="like" data-show-faces="true" data-share="false"></div> </div>
<div class="modal-footer">
<a class="btn btn-link" data-dismiss="modal" style="font-size:15px;color:#808080;cursor: pointer;text-decoration: none;">已經讚了,请稍等...</a>
</div>
</div>
</div>
</div>
<!-- Modal Window -->
<script type="text/javascript">
$(window).load(function(){
$('#formSaludo').modal('show');
});
</script>

Use setTimeout() function
$(window).load(function(){
setTimeout(function(){ $('#formSaludo').modal('show'); }, 5000);
});

You will want to do something like this
var show = function(){
$('#formSaludo').modal('show');
};
$(window).load(function(){
var timer = window.setTimeout(show,5000);
});
https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setTimeout

Try this:
$('#formSaludo').delay(5000).modal('show');
EDIT:
I'm not sure if the delay function will work in this case. You can always use setTimeout like others have suggested.

Try This Code
setTimeout(function(){
$('#formSaludo').modal(); },
5000);
Hope it will be work.

Related

why when i use modal fade as i suppose to it did not work?

I've been working with modal fade and it didn't work
what suppose happen is adding a new grade in partial view so i decided to work with modal fade and i didn't know anything about it.
so there is the code and please help me to fix the problem
<p>#* Button *#
New Grades
</p>
<div class="modal fade" id="mymodal1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
×
<h3 class="modal-title">Add/Edit Grades</h3>
</div>
<div class="modal-body" id="myModalBodyDiv1">
<div style="text-align:center;display:none" id="loaderDiv">
<img src="~/Image/load2.gif" width="150" />
</div>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
<script>
var AddEdit = function (id) {
var url = "/Grades/AddEditGrade/" + id;
$("#myModalBodyDiv1").load(url, function () {
$("mymodal1").modal("show");
})
}
</script>
any idea how can i fix it ??
you are missing ID selector symbol in this line:
wrong: $("mymodal1").modal("show");
modified: $("#mymodal1").modal("show");

Modal for multiple images on one page

*Repeat Post
I have searched around and found a potential answer to adding a modal to one page for multiple images. This solution I found is giving me my modal but when the modal pops up the image is not showing and the console showing the "img src" is "(unknown)".
The solution I found:
$(document).ready(function () {
$('#myModal').on('show.bs.modal', function (e) {
var image = $(e.relatedTarget).attr('src');
$(".img-responsive").attr("src", image);
});
});
<img id="1" data-toggle="modal" data-target="#myModal" class="image" src="/images/hieroglyphics/9.jpg">
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<img class="img-responsive" src=""/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Any help would be greatly appreciated! Thanks!
I can confirm that your code seems to work correctly.
here is a fiddle with your code.
https://jsfiddle.net/jammer99/y0w1t95u/
Are you sure there isn't anything else opening the modal?
$(document).ready(function () {
$('#myModal').on('show.bs.modal', function (e) {
var image = $(e.relatedTarget).attr('src');
$(".img-responsive").attr("src", image);
});
});
can you paste a screenshot of your console? to check if there are any other errors?

how can i create a modal by remote and initial textbox in the same function using jquery?

script.js
function change_val(){
$("#remoteModal2").modal({
remote:"modals/edit_text_value.html",
show:true
});
$("#my_textbox").val("this is a text")
}
index.html
<div class="modal fade" id="remoteModal2" tabindex="-1" role="dialog" aria-labelledby="remoteModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
<button onclick="change_val()" >show modal</button>
edit_text_value.html
<div class="modal-header">
header
</div>
<div class="modal-body">
<textarea id="my_textbox"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">cancel</button>
</div>
The problem is that I must run it twice to make it work !
How to do all the operations in a function ?
shown.bs.modal OR show.bs.modal
When we use this code :
.on('shown.bs.modal')
.
$('#remoteModal2').on('shown.bs.modal', function (e) {
$("#my_textbox").val("this is a new text);
return e.preventDefault();
});
import modal (remote "edit_text_value.html")
Open modal
After the completion of loading and effects , change value..
show new value...
But this code :
.on('show.bs.modal')
Apply the changes ,Before reading the tags and remote the modal...
Therefore, it is not effective.
Because it can not find this ID (#my_textbox) Before remote modal page!
more information...

How to close Bootstrap 3 modal programmatically on AJAX success?

I have a code that what I wanted to do is to close the modal on ajax success. This is my code:
script
success: function() {
console.log("delete success");
$('#deleteContactModal').modal('hide');
$( "#loadContacts" ).load( "/main/loadContacts" );
}
html
<div class="modal fade" id="deleteContactModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<!--everything goes here -->
</div>
</div>
</div>
Everything just works except when the code $('#deleteContactModal').modal('hide'); triggers, it just shows a black faded screen like this:
The modal closes but the black faded color is still present. Am I missing something here? Thank you in advance.
I'm using bootstrap 3.3.
try to add this attribute with your modal div aria-hidden="true"
eg:
<div aria-hidden="true" class="modal fade" id="deleteContactModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
Here is my working example
<div class="modal fade" id="copy_course_modal" tabindex="-1" role="dialog" aria-labelledby="copycourse" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="purchaseLabel">Copy Chapter</h4>
</div>
<div class="modal-body">
Modal body content here
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclick="saveCopiedCourse()">Copy Course</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
and on success doing same.
$("#copy_course_modal").modal('hide');
I have the same exact problem and the only way I could find to work is to individually remove the parts of the modal is generating. Just put this function in yous js and make an onclick event at your button in your html or js. Hope I helped.
function hideModal(){
$(".modal").removeClass("in");
$(".modal-backdrop").remove();
$('body').removeClass('modal-open');
$('body').css('padding-right', '');
$(".modal").hide();
}
Try:
$(".modal.in").modal("hide");
This will hide the currently active modal.
Ran into this issue myself in a similar situation.
It seems to be related to the asynchronous nature of javascript + bootstrap animations.
It's a dirty, dirty hack, but wrapping the call to 'hide' in a timeout made it work for me:
setTimeout( function(){$("#myModal").modal('hide')}, 300 );
If employing this "solution" to the problem, you may need to adjust the timeout value. Bootstrap animations seem to take around 125 - 200 ms, so 300 provides a nice safety buffer.
$('#deleteContactModal').modal('hide')
Find this link
https://getbootstrap.com/docs/3.3/javascript/#modal-hide
It gives detail https://getbootstrap.com/docs/3.3/javascript/#modal-hide regarding model window
Simple programmatically click close button of dialog.
$("button[data-dismiss=\"modal\"]").click();
This will automatically close dialog.
This issue will solve by hiding individual elements of modal.
Such as :
$("#modal").modal('hide');
$('body').removeClass('modal-open');
$(".modal-backdrop").remove();
I tried several of the proposed solutions and the only one that worked for me was:
$(".modal.in").modal('hide');
Some did clear the modal and the backdrop but then it did not redisplay on
subsequent invocations.
$.ajax({
type: "POST",
url: "admin/pc-item-insert.php",
data: $(this).serialize(),
success: function(data) {
$("#showinfo").html(data);
$(".modal").modal("hide");
},
});
This is just a timing problem. The Fade animation takes time and javascript cant close it. just cancel the fade animation and it works properly!
<div class="modal" tabindex="-1" role="dialog" aria-labelledby="...">
...
</div>
(Not use class="modal fade", jus class="modal")
None of these options worked for me apart from the one that said don't use modal fade. However I wanted to use modal fade. My code was making an ajax call to save changes, and then on success was doing this:
$('#myModal').modal('hide');
doRefresh();
The problem was doRefresh was then updating the page under the modal. If I removed the doRefresh, it worked. So what I ended up doing was this:
$('#myModal').modal('hide');
setTimeout(doRefresh, 500);
To clear the backdrop
$(".modal-backdrop").toggleClass("hide, show");
tested in bs4
I define my modal :
<div class="modal fade" aria-hidden="true" role="dialog" id="modal" tabindex="-1">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<button id="btnCloseModal" hidden type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>Waiting</strong>
</div>
<div class="modal-content">
<div>
Please don't close your tab!
</div>
<div class="d-flex justify-content-center">
<div class="spinner-border" role="status">
<span class="sr-only">Loading...</span>
</div>
</div>
</div>
<div class="modal-footer">
<strong>Loading...</strong>
</div>
</div>
</div>
</div>
then I create function :
var StopLoadingAnimation = function () {
$('#modal').on('show.bs.modal', function (e) {
console.log("trigger show");
$("#btnCloseModal").trigger("click");
});
$('#modal').on('shown.bs.modal', function (e) {
console.log("trigger");
$("#btnCloseModal").trigger("click");
});
$('#modal').on('hidden.bs.modal', function (e) {
console.log("event");
$(e.currentTarget).off('shown');
$(e.currentTarget).off('show');
});
$("#btnCloseModal").trigger("click");
}
My idea is after ajax success is will call function StopLoadingAnimation what will trigger event click on element btnCloseModal ( It like you click button btnCloseModal when you closing modal )

Data-remote modal-body loading bar for bootstrap 3

Below is my Code for edit vehicle modal call
<?php for($i=0;$i<count($vhcl);$i++){
echo "<a data-toggle='modal' href=' ' data-remote='using/vehicles/vehicle-form.php?id=".$vhcl[$i]['mvid']."&m=e' data-target='#vehicle-modal'><i class='icon-pencil color-orange'></i></a>";
} ?>
<div class="modal fade" id="vehicle-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
Now when I first time click on this link it takes few seconds and modal appears with 1st vehicle edit data,
But now when I click on 2nd vehicle modal appears instantly with same data, and its refreshed after 2 3 seconds( it may also take more time in slow connections ).
So question is can we have any modal-body load completed check ?
Until the modal body is loaded with new refreshed data, loading image should be there in modal body and then the actual data.
You can do the following:
Try to call modal using javascript instead of attributes. Before modal start to download remote path content you can delete the modal content.
This is example in JSFiddle
Html:
<a href='#' class="open-modal" data-remote='http://fiddle.jshell.net/bHmRB/22/show/'><i class='icon-pencil color-orange'></i></a>
<br/>
<a href='#' class="open-modal" data-remote='http://fiddle.jshell.net/bHmRB/40/show/'><i class='icon-pencil color-orange'></i></a>
<div id="modal"></div>
Javascript:
$(function(){
$("a.open-modal").click(function(e){
e.preventDefault();
var modal=$("#modal");
modal.empty();
modal.append("<div class='modal fade modal-dialog' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'></div>");
$('.modal-dialog').modal({
remote:$(e.currentTarget).attr("data-remote")
});
});
});
EDIT:
You also can place some image or text like "Loading..." while loading.
This is example in JSFiddle
Html:
<span id="onLoad" style="position:absolute;left:100px;top:100px;z-index:1000000;" hidden>Loading...</span>
<a href='#' class="open-modal" data-remote='http://fiddle.jshell.net/bHmRB/22/show/'><i class='icon-pencil color-orange'></i></a>
<br/>
<a href='#' class="open-modal" data-remote='http://fiddle.jshell.net/bHmRB/40/show/'><i class='icon-pencil color-orange'></i></a>
<div class='modal fade' id='modal-dialog' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'></div>
Javascript:
$(function(){
$("a.open-modal").click(function(e){
$('#onLoad').show();
e.preventDefault();
var modal=$("#modal-dialog");
modal.empty();
modal.modal({
}).load($(e.currentTarget).attr("data-remote"), function (e) {
$('#onLoad').hide();
});
});
});
Here is the easier solution based on the #Nicolai's solution
I was experiencing issues with the solutions above, that's I'm also posting my solution, so someone can get it correctly in future :)
HTML:
<a class="open-custom-modal" data-remote="/url/1" data-toggle="modal" data-target="#custom_modal">URL 1</a>
<br />
<a class="open-custom-modal" data-remote="/url/2" data-toggle="modal" data-target="#custom_modal">URL 1</a>
<div class="modal fade" id="custom_modal" tabindex="-1" role="dialog" aria-labelledby="custom_modal_label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
</div>
</div>
</div>
</div>
JavaScript:
$(function() {
$("a.open-custom-modal").click(function(e) {
e.preventDefault();
$('#custom_modal').modal({
}).find('.modal-body').load($(e.currentTarget).attr("data-remote"), function(e) {
//loading completes here
});
});
});

Categories

Resources