views:

60

answers:

2

I have this piece of code to open up a JQuery dialog with specific controls according to 2 links. Now it works fine on the 1st attempt. But the 2nd time when I click the link to load the JQuery dialog, it opens a blank dialog. On closing it and clicking the link again it opens the right dialog. On repeated trials it throws me an error in the JQuery 1.3.2.js file.

Code given below:

 $('#div1').dialog("destroy");            

 if (criteria1== "L") {
    $("#div1").html("<iframe id='dialogFrame1' src='../WebPages/abc.aspx'        
    Height='100%' Width='100%' frameborder='0'></iframe>");
  }
 else {
    $("#div1").html("<iframe id='dialogFrame2' src='../WebPages/abc1.aspx'    
  Height='100%' Width='100%' frameborder='0'></iframe>");
 }

 $('#div1').dialog(
 {

  height: 220,
  title: "Title",
  width: 500,
  modal: true,
  beforeclose: function (event, ui) {

      $("#div1").html("");
      $("#div1")[0].innerHTML = "";
    }

 });

 $('#div1').parent().appendTo($("form:first"));
 $('#div1').dialog('open');

Am i using the wrong JQuery file? Any views on how to fix this issue?

A: 

EDIT: Misunderstood the problem, 2nd attempt:

Can't reproduce your problem, have tried at http://jsfiddle.net/GYxwG/3/. I did have to remove your $('#div1').parent().appendTo($("form:first")); however, as it wouldn't work at all with this in place. This statement might not do what you think it does, cause once you call the .dialog() on the div, the div is moved out from its position and wrapped in all sorts of dialog-goodness. So .parent() refers to one of the inner wrappers of the dialog. Not the entire dialog, nor the parent element in the original document.

It therefore makes little sense to be moving the closest parent to the div for the dialog, and causes "unspecified" behaviour, perhaps you're seeing some edge case of that?

Alexander Sagen
All this code is inside a function that is called on the click of 2 seperate LinkButtons. So it does set the iframe to the div each time the function is called.
deepa
Updated, not entirely reproducable. Oh and yes, error or URL would be nice
Alexander Sagen
If I comment out the beforeclose call, it doesnt show me the empty dialog but shows me the last actioned display every alternate time. Pls let me know if i need to clarify more. I'm new to JQuery so am i missing something very basic?
deepa
i do not get any error, just that the functionality doesn't work right. This entire code given above is inside a js function and i call this from 2 different linkbuttons passing criteria1 as a parameter.
deepa
Did you look at what I wrote? I didn't mention beforeclose call, only $('#div1').parent().appendTo($("form:first")); which won't do what you expect. Tried to just comment out that? The link to jsfiddle I posted contains a working example, should be possible for you to go from there?
Alexander Sagen
Yes. I tried out commenting that line, but still it shows me the last actioned page. I point to the same url and pass them different parameters and depending on those parameters set the controls enable/disable property. For eg: I say lock A1, unlock A2 in my dialog and update it, on edit of that page it must show me Unlock A1, lock A2, but it shows me the original page first and the desired output when i close this dialog and re-open it again. On debugging the values passed are right.
deepa
Do you have an URL? Hard to debug when I've given you a working example, and yet, it doesn't work..
Alexander Sagen
Unfortunately no, we are still in the dev state of the project and its local. Any ideas if i need to chk something.. or is it the IFrame that is causing the problem. Any way i can mail the code?
deepa
I tested my code with many combinations. Found one similarity. I have a refresh button on the parent page that refreshes the grid values that are modified in the dialog page. Now if the refresh button is clicked and then the dialog is opened it gives the alternate dialog opening with right controls prblm or else it opens the dialog with the control rightly. Has anyone faced any prblms like this.
deepa
This piece of code works: $('.ui-widget-overlay , .ui-dialog', window.parent.document).remove(); just in case anyone faces the same issues.
deepa
A: 

jsfiddle working example (it's missing the jquery css/images)

So if I might suggest to scrap the code you have given it appears to be a mess, and instead do:

$(function(){
    var urls = {
        "hackernews" : "http://news.ycombinator.com", 
        "jsfiddle" : "http://jsfiddle.net"
    };

    var dialog = $("<div style='width:50%;height:50%;'>\
                   <iframe id='dialogFrame'\
                   src='#'  frameborder='0'></iframe></div>");


     dialog.dialog({
      height: 220, 
      title: "Title",
      width: 500,
      modal: true,
      autoOpen: false
    });

    $('#btn1, #btn2').click(
        function(){
            var urlIndex = $(this).attr('rel');
            if(!urlIndex) return;
            $('#dialogFrame', dialog).attr('src', urls[urlIndex]);
            dialog.dialog('open');
         }
    );
    $('#btnClose').click(
        function(){
            dialog.dialog('close');
        }
    );

});​

HTML:

<input type="button" id="btn1" rel="jsfiddle" value="dialog1" />
<input type="button" id="btn2" rel="hackernews" value="dialog2" />
<input type="button" id="btnClose" value="Close">​

So instead of switching between dialogs and changing inner HTML, we keep one dialog constantly available, and when it is to be displayed, we set the url of the iframe and show the dialog.

All urls are kept in a associative array, and the click event-handler checks the rel-attribute of the clicked button to figure which url should be displayed.

Makes sense?

You obviously need to change the URLs and attributes of HTML and dialo-properties.

Alexander Sagen
Ok. The buttons are froma gridview and due to my lack of knowledge and time crunch I couldn't find my way to replace the #btn1,#btn2 with the grid buttons. So what i did was I've used part of the code you gave, i.e the var dialog = {} and the dialog.dialog() in the function i call on click of the linkbuttons in the grid. And then with an if-else condition deteremine the src value and set it to the src like you have shown. So it does work now :). Thanks a lot. And i think that creating the div runtime solved the problem rather. Is it right?
deepa
Glad I could help :) Not entirely sure what you're original issue was, but this is a cleaner solution regardless.
Alexander Sagen
I have Save and Cancel buttons in the dialog that are aspx buttons. How do I close the dialog on click on them. i was using window.parent.$("#popupFinTrans").dialog('close'); but now that nolonger works.
deepa