Simplemodal: Form Submit vs Close in iFrame - javascript

Got a popup legal notice inside an simplemodal window (iframe) that's working exactly as I expect it to. There's a form in the iframe that when you submit it, the modal window closes and you continue on to your destination - in this case, a "mailto:" link.
Problem is - I need a separate "close" function that doesn't open the mailto link, just closes the modal. Currently, submitting the form OR clicking the "close" link both open the "mailto" link.
What I've got so far:
Inside my iFrame to handle the form submit:
$('#alertFORM').submit(function() {
window.parent.jQuery.modal.close(true);
});
And my functions handling everything else:
$('.legalnotice').on('click', function (e) {
e.preventDefault();
var src = "email_alert.html";
var mailto = $(this).attr('href');
$.modal('<iframe src="' + src + '" height="400" width="390" style="border:0" id="legalFRAME">', {
closeHTML: "<a href='#'>Close</a>",
closeCLASS: "simplemodal-close",
containerId: "modal",
containerCss: {
backgroundColor: "#f8f8f8",
borderColor: "#f8f8f8",
height: 500,
padding: 0,
width: 400
},
overlayClose: true,
onClose: function (dialog) {
$.modal.close();
window.location.href = mailto;
}
});
});
});
I believe I understand the problem - both the form submit and the 'close' link call the onClose function. Haven't quite figured out ow to seperate the two yet so only the form submit opens the 'mailto' link.
I've tried changing the code in my iFrame:
$('#alertFORM').submit(function() {
parent.closeModal();
});
... and then back on the parent page at the top of my javascript above:
function closeModal() {
$("#modal").dialog("close");
}
... but that doesn't work.
Actively working on it. Suggestions most appreciated.

Got it finally! Inside the file in my iFrame:
$('#alertFORM').submit(function() {
window.showEmail = "true";
window.parent.jQuery.modal.close(true);
});
$( ".modalCloseImg" ).on('click', function (e) {
window.parent.jQuery.modal.close(true);
});
... and back on the parent page:
onClose: function (dialog) {
window.redirectEmail = window.legalFRAME.showEmail;
if(window.redirectEmail == "true")
{
window.location.href = mailto;
window.legalFRAME.showEmail = "false";
window.redirectEmail = "false";
}
$.modal.close();
}

Related

The jquery dialogbox didn't close when it is opened on usercontrol page

I used the jQuery dialog. The parent has user control which has an image to open the popup page when it is on click. The aspx page has cancel button to close jquery method and it is worked. I added the my jquery files on the parent page. I put the dialog div on parent page.
The problem is for closing the jquery dialog box and reloading the parent. If I added my jquery files on the header on the popup page, the function is called but the error is:
JavaScript runtime error: Object doesn't support property or method
'dialog'. Also the Cancel button didn't close the jquery.
However, when I uncomment my jquery files on the popup page, the cancel button works. But the another button which close the popup and reload parent page, the jquery method is not called and the popup page reload and not closed.
There is my code in jQuery
function openmodel(url, name, width, height) {
var maxHeight = dialogMaxHeight(height);
var dialogHeight = height;
if (height > maxHeight)
dialogHeight = maxHeight;
$('#dialog-model').dialog({
my: "center",
at: "center",
of: window,
autoOpen: false,
resizable: true,
max_height:'auto',
height: 'auto',
width: width,
title: name,
modal: true,
draggable: true,
open: function( ) {
$(this).load(url);
},
});
$('#dialog-model').dialog('open');
return false;
}
function CloseDialogmodel() {
$('#dialog-model').dialog({
autoOpen: false,
resizable: true,
title: name,
modal: true,
});
$('#dialog-model').dialog('close');
}
function CloseDialogModelAndReloadParent() {
CloseDialogmodel(); }
The code behind on aspx page:
Private Sub btnDone_Click(sender As Object, e As EventArgs) Handles btnDone.Click
'do something on server
Dim cs As ClientScriptManager = Page.ClientScript
cs.RegisterStartupScript(Page.GetType (), "closeandload", "CloseDialogBoxAndReloadParent();", True)
End Sub
Hope someone tell me how to resolve the problem, so I can close the popup page and reload it. Thanks in advance.
I have same issue and I handled it like below:
Child.aspx-Markup
<div class="button">
<asp:Button ID="btnDone" Text="Done" runat="server" />
<a ID="lnkClose" onclick="DecideForParent(false,true);return false;">
Close
</a>
</div>
<script type="text/javascript">
function DecideForParent(AllowReferesh, AllowClose) {
try {
if (AllowReferesh)
window.opener.HandlePopupResult();
}
catch (ex) {
}
// Close dialog
if (AllowClose)
$('#dialog-model').dialog('close');
}
</script>
Child.aspx-Code behind (C#)
private void btnDone_Click(object sender, EventArgs e)
{
// Do something ...
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallreturnToParent", "$(window).load(function () { DecideForParent(true,true);return false;});", true);
}
Parent.aspx-Markup
<script type="text/javascript">
function HandlePopupResultLURow() {
document.getElementById('<%=btnRefresh.ClientID%>').click();
}
</script>
Parent.aspx-Code behind (C#)
private void btnRefresh_Click(object sender, EventArgs e)
{
// Refresh your data
}
if i guess right, you create the dialog like this
$( "#dialog-confirm" ).dialog({
//....
modal: true,
buttons: {
"Do Action": function() {
CloseDialogBoxAndReloadParent();
},
"Cancel" : function() {
CloseDialogBox();
}
}
});
} );
if you add the element to the parameters
"Cancel" : function() {
CloseDialogBox(this);
}
you can access the right element
function CloseDialogBox(element) {
$(element).dialog('close');
}
function CloseDialogmodel () {
$('#dialog-model').dialog({ autoOpen: false, resizable: true, title: name, modal: true, });
$('#dialog-model').modal('hide');
}
Finally, I got it . I just need to respond redirect the parent page on btnDone_Click. Thanks.

Jquery onclick function not being called

I have this function in my global.js file, code below:
$("a.dialog-page").click(function(event) {
event.preventDefault();
$this = $(this);
var URL = $(this).attr('href');
var dialogbox = document.getElementById('dialog');
var dialogOptions = {
width: 500,
height: 200,
modal: true,
close: function(event, ui){
$('#dialog').empty();
}
};
if(dialogbox==null) {
$this.after("<div id=\"dialog\"></div>");
}
jQuery('#dialog').load(URL + " #content").dialog(dialogOptions);
});
I have dynamically generated HTML with the following markup:
<div id="dynamic-id">
<a class="dialog-page" href="/test/test.php">Link to test</a>
</div>
Here is the click trigger for that link coming from my local.js file:
$('#dynamic-id').on('click', 'a.dialog-page', function(event){
event.preventDefault;
});
The problem is when clicking on the link, it is not calling ("a.dialog-page").click event which would load up my modal window.
How can I address this?
Thanks
Cheers
Since #dynamic-id is also generated dynamically, you need to use other static parent like body:
$('body').on('click', 'a.dialog-page', function(event){
event.preventDefault;
});
Element #dynamic-id is also generated dynamically.
So when you are binding the element the element #dynamic-id doesnot exist in DOM while you are binding the event to it .
TO fix this you can use as follows
$(document).on('click', 'a.dialog-page', function(event){
alert("Link clicked");
event.preventDefault;
});
Try this -
$('#dynamic-id').on('click', 'a.dialog-page', function(event){
event.preventDefault;
$this = $(this);
var URL = $(this).attr('href');
var dialogbox = document.getElementById('dialog');
var dialogOptions = {
width: 500,
height: 200,
modal: true,
close: function(event, ui){
$('#dialog').empty();
}
};
if(dialogbox==null) {
$this.after("<div id=\"dialog\"></div>");
}
jQuery('#dialog').load(URL + " #content").dialog(dialogOptions);
});
Reason for not working -
$("a.dialog-page").click(function(event) {
and
$('#dynamic-id').on('click', 'a.dialog-page', function(event){
are basically the same. so when you are executing the second line you are overriding the previous onclick event. You can use any one of them and you should be just fine. Here is the fiddle.. http://jsfiddle.net/KytV8/
You just need to target an element that exists on pageload and use the on() function
$('#container').on('click', '.element', function(){ });
The way you were originally trying to do it, the click listener doesn't get set since when it's applied, it can't find the element it's trying to listen to.
To me it looks like you are defining the click event statically, which will never be triggered by the A tag since it's not being attached to the dynamic content, then defining and attaching a new click after the dynamic data is loaded that only stops the event. I would change it to this, as also alluded to by Pointy's comment:
<div id="dynamic-id">
<a class="dialog-page" href="/test/test.php">Link to test</a>
</div>
$('#dynamic-id').on('click', 'a.dialog-page', function(event){
event.preventDefault();
$this = $(this);
var URL = $(this).attr('href');
var dialogbox = document.getElementById('dialog');
var dialogOptions = {
width: 500,
height: 200,
modal: true,
close: function(event, ui){
$('#dialog').empty();
}
};
if(dialogbox==null) {
$this.after("<div id=\"dialog\"></div>");
}
jQuery('#dialog').load(URL + " #content").dialog(dialogOptions);
});

JQGrid onSelectRow event and jquery.simplemodal.js modal dialog

I am trying to show a modal window using jquery.simplemodal.js JQuery plugin everytime I click on a row of a JQGrid.
After doing it, once the user click "Yes" on the modal window I show a document on an Iframe using the info retrieved from my row, if they click "No" then the Iframe shows a form to edit some info related to this document. The idea is to keep the Iframe content blank until the user clicks on one of the buttons. To accomplish this I am using JQGrid onSelectRow event as follows:
onSelectRow: function () {
var id = $("#tabResBusUbi").getGridParam('selrow');
var row = $('#tabResBusUbi').jqGrid('getRowData', id);
viewDocument = false;
myconfirm('Open the document view?', function () {
viewDocument = true;
});
if (viewDocument) {
$('#ifrShowPdf').ready(function () {
$('#divLoading').css('display', 'none');
});
$('#ifrShowPdf').attr('src', 'resource\\' + row.filepath);
$('#ifrShowPdf').css('display', 'block');
} else {
var rnd = Math.floor(Math.random() * 11);
$('#ifrShowPdf').attr('src', 'document.asp?r=' + rnd + '&o=d&a=a&p=' + id);
$('#ifrShowPdf').css('display', 'block');
}
return false;
}
function myconfirm(message, callback) {
$('#confirm').modal({
closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
position: ["20%", ],
overlayId: 'confirm-overlay',
containerId: 'confirm-container',
onShow: function (dialog) {
var modal = this;
$('.message', dialog.data[0]).append(message);
$('.yes', dialog.data[0]).click(function () {
if ($.isFunction(callback)) {
callback.apply();
$('#confirm-overlay').css('z-index', 5000);
}
modal.close();
});
}
});
}
After I click on the row, the modal popups, but also the Iframe shows the edit document form even when I haven't clicked on any of the option buttons. If I click the "Yes" option after, the document is viewed but, this is not what I intend to do.
I would appreciate any suggestions to make this work.

Page Refreshes before jQuery dialog is closed

I created a function that replaces the window.alert function with my own function that utilizes jquery.dialog. The problem is that in some functions I call that function and reload the page right afterwards. It is supposed to refresh it when user clicks "OK", but it is reloading the page by itself before I even click "OK".
Here's an example of call sequence in a function:
function UpdateCertSucccess(result) {
customAlert("Hello World");
window.location.href = "./SomePage.aspx";
}
And here's my defined customAlert()
function customAlert(message) {
if (!isOpen) {
$('#error-message-dialog').dialog({
autoOpen: false, bgiframe: true, position: ['center', 100], modal: true, zIndex: '6000', title: 'R+L Carriers Message', width: 475, height: 250,
buttons: {
"OK": function () {
$(this).dialog("close");
isOpen = false;
}
}
});
var elements = message.split("|");
$('#spMessage').text(elements[0]);
$('#spCode').text(elements[1]);
$('#spTime').text(elements[2]);
$('#spServer').text(elements[3]);
$('#error-message-dialog').dialog('open');
isOpen = true;
}
else {
$('#spMessage').append("<br /><br />");
$('#spMessage').append(message);
}
return false;
};
What could be causing the page reload before I close the dialog and how can I fix it?
Thanks!
remove 'window.location.href = "./SomePage.aspx";' from 'UpdateCertSucccess()' function and place it in the function call after Ok click (ok click callback function where you closing the dialog box)
Only browser popups (alert, confirm...) can suspend the execution of a function. You can't achieve the same effect yourself, so you'll need to improve your code, add an "onclose" callback option or something.

jQuery UI - Close Dialog When Clicked Outside

I have a jQuery UI Dialog that gets displayed when specific elements are clicked. I would like to close the dialog if a click occurs anywhere other than on those triggering elements or the dialog itself.
Here's the code for opening the dialog:
$(document).ready(function() {
var $field_hint = $('<div></div>')
.dialog({
autoOpen: false,
minHeight: 50,
resizable: false,
width: 375
});
$('.hint').click(function() {
var $hint = $(this);
$field_hint.html($hint.html());
$field_hint.dialog('option', 'position', [162, $hint.offset().top + 25]);
$field_hint.dialog('option', 'title', $hint.siblings('label').html());
$field_hint.dialog('open');
});
/*$(document).click(function() {
$field_hint.dialog('close');
});*/
});
If I uncomment the last part, the dialog never opens. I assume it's because the same click that opens the dialog is closing it again.
Final Working Code
Note: This is using the jQuery outside events plugin
$(document).ready(function() {
// dialog element to .hint
var $field_hint = $('<div></div>')
.dialog({
autoOpen: false,
minHeight: 0,
resizable: false,
width: 376
})
.bind('clickoutside', function(e) {
$target = $(e.target);
if (!$target.filter('.hint').length
&& !$target.filter('.hintclickicon').length) {
$field_hint.dialog('close');
}
});
// attach dialog element to .hint elements
$('.hint').click(function() {
var $hint = $(this);
$field_hint.html('<div style="max-height: 300px;">' + $hint.html() + '</div>');
$field_hint.dialog('option', 'position', [$hint.offset().left - 384, $hint.offset().top + 24 - $(document).scrollTop()]);
$field_hint.dialog('option', 'title', $hint.siblings('label').html());
$field_hint.dialog('open');
});
// trigger .hint dialog with an anchor tag referencing the form element
$('.hintclickicon').click(function(e) {
e.preventDefault();
$($(this).get(0).hash + ' .hint').trigger('click');
});
});
Sorry to drag this up after so long but I used the below. Any disadvantages? See the open function...
$("#popup").dialog(
{
height: 670,
width: 680,
modal: true,
autoOpen: false,
close: function(event, ui) { $('#wrap').show(); },
open: function(event, ui)
{
$('.ui-widget-overlay').bind('click', function()
{
$("#popup").dialog('close');
});
}
});
Forget using another plugin:
Here are 3 methods to close a jquery UI dialog when clicking outside popin:
If the dialog is modal/has background overlay: http://jsfiddle.net/jasonday/6FGqN/
jQuery(document).ready(function() {
jQuery("#dialog").dialog({
bgiframe: true,
autoOpen: false,
height: 100,
modal: true,
open: function(){
jQuery('.ui-widget-overlay').bind('click',function(){
jQuery('#dialog').dialog('close');
})
}
});
});
If dialog is non-modal Method 1: method 1: http://jsfiddle.net/jasonday/xpkFf/
// Close Pop-in If the user clicks anywhere else on the page
jQuery('body')
.bind(
'click',
function(e){
if(
jQuery('#dialog').dialog('isOpen')
&& !jQuery(e.target).is('.ui-dialog, a')
&& !jQuery(e.target).closest('.ui-dialog').length
){
jQuery('#dialog').dialog('close');
}
}
);
Non-Modal dialog Method 2: http://jsfiddle.net/jasonday/eccKr/
$(function() {
$( "#dialog" ).dialog({
autoOpen: false,
minHeight: 100,
width: 342,
draggable: true,
resizable: false,
modal: false,
closeText: 'Close',
open: function() {
closedialog = 1;
$(document).bind('click', overlayclickclose);
},
focus: function() {
closedialog = 0;
},
close: function() {
$(document).unbind('click');
}
});
$('#linkID').click(function() {
$('#dialog').dialog('open');
closedialog = 0;
});
var closedialog;
function overlayclickclose() {
if (closedialog) {
$('#dialog').dialog('close');
}
//set to one because click on dialog box sets to zero
closedialog = 1;
}
});
Check out the jQuery Outside Events plugin
Lets you do:
$field_hint.bind('clickoutside',function(){
$field_hint.dialog('close');
});
Just add this global script, which closes all the modal dialogs just clicking outsite them.
$(document).ready(function()
{
$(document.body).on("click", ".ui-widget-overlay", function()
{
$.each($(".ui-dialog"), function()
{
var $dialog;
$dialog = $(this).children(".ui-dialog-content");
if($dialog.dialog("option", "modal"))
{
$dialog.dialog("close");
}
});
});;
});
$(".ui-widget-overlay").click (function () {
$("#dialog-id").dialog( "close" );
});
Fiddle showing the above code in action.
I had to do two parts. First the outside click-handler:
$(document).on('click', function(e){
if ($(".ui-dialog").length) {
if (!$(e.target).parents().filter('.ui-dialog').length) {
$('.ui-dialog-content').dialog('close');
}
}
});
This calls dialog('close') on the generic ui-dialog-content class, and so will close all dialogs if the click didn't originate in one. It will work with modal dialogs too, since the overlay is not part of the .ui-dialog box.
The problem is:
Most dialogs are created because of clicks outside of a dialog
This handler runs after those clicks have created a dialog and bubbled up to the document, so it immediately closes them.
To fix this, I had to add stopPropagation to those click handlers:
moreLink.on('click', function (e) {
listBox.dialog();
e.stopPropagation(); //Don't trigger the outside click handler
});
This question is a bit old, but in case someone wants to close a dialog that is NOT modal when user clicks somewhere, you can use this that I took from the JQuery UI Multiselect plugin. The main advantage is that the click is not "lost" (if user wants to click on a link or a button, the action is done).
$myselector.dialog({
title: "Dialog that closes when user clicks outside",
modal:false,
close: function(){
$(document).off('mousedown.mydialog');
},
open: function(event, ui) {
var $dialog = $(this).dialog('widget');
$(document).on('mousedown.mydialog', function(e) {
// Close when user clicks elsewhere
if($dialog.dialog('isOpen') && !$.contains($myselector.dialog('widget')[0], e.target)){
$myselector.dialog('close');
}
});
}
});
You can do this without using any additional plug-in
var $dialog= $(document.createElement("div")).appendTo(document.body);
var dialogOverlay;
$dialog.dialog({
title: "Your title",
modal: true,
resizable: true,
draggable: false,
autoOpen: false,
width: "auto",
show: "fade",
hide: "fade",
open:function(){
$dialog.dialog('widget').animate({
width: "+=300",
left: "-=150"
});
//get the last overlay in the dom
$dialogOverlay = $(".ui-widget-overlay").last();
//remove any event handler bound to it.
$dialogOverlay.unbind();
$dialogOverlay.click(function(){
//close the dialog whenever the overlay is clicked.
$dialog.dialog("close");
});
}
});
Here $dialog is the dialog.
What we are basically doing is to get the last overlay widget whenever this dialog is opened and binding a click handler to that overlay to close $dialog as anytime the overlay is clicked.
no need for the outside events plugin...
just add an event handler to the .ui-widget-overlay div:
jQuery(document).on('click', 'body > .ui-widget-overlay', function(){
jQuery("#ui-dialog-selector-goes-here").dialog("close");
return false;
});
just make sure that whatever selector you used for the jQuery ui dialog, is also called to close it.. i.e. #ui-dialog-selector-goes-here
This doesn't use jQuery UI, but does use jQuery, and may be useful for those who aren't using jQuery UI for whatever reason. Do it like so:
function showDialog(){
$('#dialog').show();
$('*').on('click',function(e){
$('#zoomer').hide();
});
}
$(document).ready(function(){
showDialog();
});
So, once I've shown a dialog, I add a click handler that only looks for the first click on anything.
Now, it would be nicer if I could get it to ignore clicks on anything on #dialog and its contents, but when I tried switching $('*') with $(':not("#dialog,#dialog *")'), it still detected #dialog clicks.
Anyway, I was using this purely for a photo lightbox, so it worked okay for that purpose.
The given example(s) use one dialog with id '#dialog', i needed a solution that close any dialog:
$.extend($.ui.dialog.prototype.options, {
modal: true,
open: function(object) {
jQuery('.ui-widget-overlay').bind('click', function() {
var id = jQuery(object.target).attr('id');
jQuery('#'+id).dialog('close');
})
}
});
Thanks to my colleague Youri Arkesteijn for the suggestion of using prototype.
This is the only method that worked for me for my NON-MODAL dialog
$(document).mousedown(function(e) {
var clicked = $(e.target); // get the element clicked
if (clicked.is('#dlg') || clicked.parents().is('#dlg') || clicked.is('.ui-dialog-titlebar')) {
return; // click happened within the dialog, do nothing here
} else { // click was outside the dialog, so close it
$('#dlg').dialog("close");
}
});
All credit goes to Axle
Click outside non-modal dialog to close
For those you are interested I've created a generic plugin that enables to close a dialog when clicking outside of it whether it a modal or non-modal dialog. It supports one or multiple dialogs on the same page.
More information here: http://www.coheractio.com/blog/closing-jquery-ui-dialog-widget-when-clicking-outside
Laurent
I use this solution based in one posted here:
var g_divOpenDialog = null;
function _openDlg(l_d) {
// http://stackoverflow.com/questions/2554779/jquery-ui-close-dialog-when-clicked-outside
jQuery('body').bind(
'click',
function(e){
if(
g_divOpenDialog!=null
&& !jQuery(e.target).is('.ui-dialog, a')
&& !jQuery(e.target).closest('.ui-dialog').length
){
_closeDlg();
}
}
);
setTimeout(function() {
g_divOpenDialog = l_d;
g_divOpenDialog.dialog();
}, 500);
}
function _closeDlg() {
jQuery('body').unbind('click');
g_divOpenDialog.dialog('close');
g_divOpenDialog.dialog('destroy');
g_divOpenDialog = null;
}
I had same problem while making preview modal on one page. After a lot of googling I found this very useful solution. With event and target it is checking where click happened and depending on it triggers the action or does nothing.
Code Snippet Library site
$('#modal-background').mousedown(function(e) {
var clicked = $(e.target);
if (clicked.is('#modal-content') || clicked.parents().is('#modal-content'))
return;
} else {
$('#modal-background').hide();
}
});
İt's simple actually you don't need any plugins, just jquery or you can do it with simple javascript.
$('#dialog').on('click', function(e){
e.stopPropagation();
});
$(document.body).on('click', function(e){
master.hide();
});
I don't think finding dialog stuff using $('.any-selector') from the whole DOM is so bright.
Try
$('<div />').dialog({
open: function(event, ui){
var ins = $(this).dialog('instance');
var overlay = ins.overlay;
overlay.off('click').on('click', {$dialog: $(this)}, function(event){
event.data.$dialog.dialog('close');
});
}
});
You're really getting the overlay from the dialog instance it belongs to, things will never go wrong this way.
With the following code, you can simulate a click on the 'close' button of the dialog (change the string 'MY_DIALOG' for the name of your own dialog)
$("div[aria-labelledby='ui-dialog-title-MY_DIALOG'] div.ui-helper-clearfix a.ui-dialog-titlebar-close")[0].click();
Smart Code:
I am using following code so that every thing remains clear and readable.
out side body will close the dialog box.
$(document).ready(function () {
$('body').on('click', '.ui-widget-overlay', closeDialogBox);
});
function closeDialogBox() {
$('#dialog-message').dialog('close');
}
I ended up using this code which should work on any open dialogs on the page, ignores clicks on tooltips, and cleans up the resources of the dialog being closed as well.
$(document).mousedown(function(e) {
var clicked = $(e.target); // get the element clicked
if (clicked.is('.ui-dialog-content, .ui-dialog-titlebar, .ui-tooltip') || clicked.parents().is('.ui-dialog-content, .ui-dialog-titlebar, .ui-tooltip')) {
return; // click happened within the dialog, do nothing here
} else { // click was outside the dialog, so close it
$('.ui-dialog-content').dialog("close");
$('.ui-dialog-content').dialog("destroy");
$('.ui-dialog-content').detach();
}
});
I just ran across the need to close .dialog(s) with an out of element click. I have a page with a lot of info dialogs, so I needed something to handle them all. This is how I handled it:
$(document).ready(function () {
$(window).click(function (e) {
$(".dialogGroup").each(function () {
$(this).dialog('close');
})
});
$("#lostEffClick").click(function () {
event.stopPropagation();
$("#lostEffDialog").dialog("open");
};
});

Categories