Disabling window while opening popup - javascript

I've looked around this site a bit, and can't seem to find a solution that works for me...
I'm trying to disable parent window while opening a popup- I've tried:
<body onClick="func1()" onFocus="func1()">
with
editwindow = window.open("","","width=300, height=300");
with the following function in my header:
function func1(){
if (editwindow && !editwindow.closed) editwindow.focus();
}
My problem is that this method doesn't effectively "disable" the parent window. I need links and buttons not to respond when clicked on. It seems like the browser is giving preference to the links first, and then re-focuses to the popup window.
Any help would be appreciated.

Instead of reinventing the wheel, and dealing with pop-up blocker headaches in the future ruining your User Experience, you might consider using jQuery UI's modal dialog.
<script>
$(function() {
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Delete all items": function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
</script>

Related

Getting wrong length on dialog

I'm working with jquery and I'm trying to listen for how may are there open after I close a dialog, but I'm getting how many where open.
For example: if I have 4 and I close one it returns 4 when I expect to get 3, and at the end when I have only 1 open it returns 2, and at the end the when I close the last one return 1.
Am I doing right? I try look in the docs and try to look for something like afterClose but there is not.
$( ".test" ).dialog({
autoOpen: false,
height: "auto",
width: "auto",
modal: true,
close: function(e){
$(this).destroy();
// This returns wrong lenght
console.log($('.test').length);
if($('.test').length === 1) {
console.log($('.test a').text());
}
}
});
How are you getting those numbers? Unless you are destroying dialogs calling
$('.test').length
is always going to return the number of classes of test on the dom. Are you using .remove() or something to remove the dialogs? More information is needed.
When you close a dialog it does not remove it from the DOM. It just hides it, you can see the hidden html at the bottom of the page. The proper way to use multiple dialogs is to use instances of dialogs. but that shit is complicated. Post some more of your code, specifically how you are setting up your html and if you are some how deleting dialogs.
Also (since i cant comment yet), the close event fires as the window is closing but before the window is hidden, this allows you two places to cancel the close event, beforeClose (typically for some validation), and close (maybe where you do some population of objects or something).
Try changing the jQuery selector to search for visible items:
$( ".test" ).dialog({
autoOpen: false,
height: "auto",
width: "auto",
modal: true,
close: function(e){
// This returns wrong lenght
console.log($('.test:visible').length);
if($('.test:visible').length === 1) {
console.log($('.test a').text());
}
}
});
Source
$( ".test" ).dialog({
autoOpen: false,
height: "auto",
width: "auto",
modal: true,
close: function(e){
$(this).destroy();
// This returns wrong lenght
console.log($('.test').length);
if($('.test').length === 1) {
console.log($('.test a').text());
}
}
});

multiple dialog box within same page

Requirement is to show dialog box on click of a button.I have created dialog box using jQuery UI. Please find the code here http://jsfiddle.net/M4QM6/32/.
ISsue is i have single function for creating dialog box, how can i show multiple dialog box within same page with each dialog box displaying different data,
When i click on dialog2 button, i need to show a dialog box which has textArea and a submit button.Please suggest.
Below is the sample code:
$(function() {
$("#dialog").dialog({
autoOpen: false,
resizable: true,
width:"750",
height:300,
modal: true,
buttons: {
"Close": function() {
$(this).dialog("close");
}
}
});
});
You could go a couple routes. Since your need for dialog content is pretty specific (textarea control - first dialog pops second dialog - etc), I would hard-code the needed divs on the page. So, make a "#textAreaDialog" div and put the needed controls in it ad set its style to display:none.
Next, modify your function to accept parameters (the name of the div that should be popped, the funciton to execute if "OK" is clicked - and the function to execute if "Cancel" is clicked), so you're not limited to using #dialog for all of your modals and you can finely control what happens when each button is clicked (not always just closing the dialog. Then, set event handlers for the click events of the buttons you need, and call your dialog accordingly.
html:
<input type="button" id="btnPopFirstModal" Value="Open First Modal"/>
<div id="divFirstModal" style="display:none;">
Here is the content for the first modal
</div>
<div id="divSecondModal" style="display:none;">
Here is the content for the second modal
</div>
Javascript functions:
function PopDialog(divToPop, OkFunction, CancelFunction)
{
$("#" + divToPop).dialog({
autoOpen: false,
resizable: true,
width:"750",
height:300,
modal: true,
buttons: {
"Ok": function() {
OkFunction();
$(this).dialog("close");
},
"Cancel": function(){
CancelFunction();
$(this).dialog("close");
}
}
});
});
}
function PopSecondModal(){
PopDialog("divSecondModal", function(){ put code for OK Click here}, function(){put code for Cancel Click here});
}
Javascript event handlers:
$("#btnPopFirstModal").click(function(e){
e.preventDefault();
PopDialog("divFirstModal", PopSecondModal, function(){}); //empty function for cancel, but you can add your own code as needed
return false;
});
Remember, you can expand this as much as you want, adding more event handlers and custom divs to use for more tailored modals. Also, as you can see, you can write your OK and Cancel funcitons inline when calling the PopDialog function - or you can pass it a function name (this is preferable if you're going to reuse that function).
Here is how I did:
$(
//when JQuery is ready
funciton()
{
$('#SomeButton').on
(
'click',
function()
{
//Note that content could be anything (HTML, text...)
//This dynamicly create a div to be your dialog
$('<div>').append(content).dialog
(
{
//autoOpen: false, I removed it you can put it back in if you need it but I dont think its important for now
resizable: true,
//I remove the double quotes here because height didn't have any but maybe it was the other way around
width:750,
height:300,
//I put this on false because if two or more dialog would need to be displayed at the same time you can't have them modals.
modal: false,
buttons:
{
Close: function()
{
$(this).dialog("close");
}
},
//this is important it destroys and remove the dynamically create dialog when you close them so you don't get 20 dialog not displayed in your html markup.
close:
function()
{
$(this).dialog('destroy').remove();
}
}
);
}
);
}
);

Close dialog box from other function using jQuery

Hi I have a dialog box it shows some menus and when I click on any menu it shows another popup window. When I finished my work from popup, I want that the dialog box closes automatically.
Please suggest how can I do this.
jQuery:
function() {
$( this ).dialog( "close" );
window.parent.pmsSession.scrollposition = true;
window.parent.pmsSession.appointmenttime = fromTime;
window.parent.$("#gridcontainer").reload();
CloseModelWindow();
$( '#options' ).dialog( "close" );
}
When you finish your work-- write this
$("#idDialog").dialog("close");
it means you are manually closing the dialog
When you are done, you just call:
$("#dialog_id").dialog('close');
When you finish your work-- write this
$("#idDialog").dialog("close");

JQuery-ui Dialog: How can I prevent default action when the user clicks on close button?

i'm using JQuery-ui dialog;
i'd like to perform my custom actions when user clicks on dialog's close button [X], but i'd like to prevent the closing event too!
i tried this code without success:
$( ".selector" ).dialog({
close: function(event, ui) {
event.preventDefault();
//mycode
}
});
Even if i wrote the code above the dialog is closed bypassing my "preventDefault".
Thank you!
MV
I've been looking for an answer to this too - so far the best I've come up with is
$( ".selector" ).dialog({
beforeClose: function(event, ui) {
//mycode
return false;
}
});
$('.selector').bind('dialogbeforeclose', function(event,ui){
alert('hello');
});

jQueryUI dialog box height grows too tall

I have a jQueryUI dialog box that loads its content the moment someone opens it using the "open" event to initiate the $('#dialogDiv').load() call.
Works great except the dialog box grows extremely tall if there's a lot of content being loaded. What I want is to limit the height. The maxHeight jQueryUI dialog option would seem to work perfect, except it only takes effect the moment you resize. The initial load will grow the dialog really tall and then when you try and resize, it immediately shrinks to the maxHeight.
How can I make a dynamically loading dialog box that won't grow beyond a certain height?
Adding the CSS position:absolute;overflow:hidden for class .ui-dialog will fix the problem.
Use height option while initalization...
for eg-
<script>
$(function() {
// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
$( "#dialog" ).dialog( "destroy" );
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Delete all items": function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
</script>
here you can see "height:140"
this defines that the dilog will be of only that size, no matter how much data is inside..
for more detais about the events,options,methods download(from here), extract and check out the
jquery-ui-1.8.5.custom > development-bundle > docs > dialog.html

Categories

Resources