-1

I have declared a function for showing a dialog box in jQuery

<script type="text/javascript">
function showDialog(str,strtitle)
 {
if(!strtitle) strtitle='Error message';
$('#dialog').dialog('destroy');
$('#dialog').show();
$('#dialog').html(str);
$("#dialog").dialog({
    resizable: false,
    width: 400,
    color: '#BF5E04',
      open: function () {
                    $(this).parents(".ui-dialog:first").find(".ui-dialog
                                titlebar").addClass("ui-state-error");},

    buttons: {"Ok": function() { 
    $(this).dialog("close");    }},

    overlay: { opacity: 0.2, background: "cyan" },title:strtitle});}
   </script>

And I'm calling this function, in another javascript code:

   <script type="text/javascript">
    var myFile = document.getElementById('myfile');
    //binds to onchange event of the input field
    myFile.addEventListener('change', function() {
    //this.files[0].size gets the size of your file.
     var  size = this.files[0].size;
     document.write(showDialog('size','File Size Exceeds')); 
      });
     </script>

When I execute the function, it writes Undefined, Why the dialog box is not showing. The first function is declred in the head, and the second in the body portion.

5
  • 7
    It's writing defined because the function showDialog isn't returning anything. The document.write() is not necessary, just simply call showDialog(). Commented Apr 4, 2012 at 15:47
  • have you tried running it through something like firebug? And stepping through, incidentally: on line 13 of your first section - is that line break there in the real code, or did you put it in to make it fit more neatly? Commented Apr 4, 2012 at 15:49
  • @Ivan is correct, just one addition: you might want to defer the call to showDialog until the page is loaded: $(showDialog); . Commented Apr 4, 2012 at 15:49
  • @Ivan how can i return a value from it, I am passing values to it, to show. Commented Apr 4, 2012 at 15:51
  • At the end of the function, just write return "Some text";. Commented Apr 4, 2012 at 15:52

3 Answers 3

3

document.write() is writing what is returned by showDialog(). Since that function isn't returning anything, it will write Undefined.

The document.write() is not necessary, just simply call showDialog().

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, but after adding the code , and what you said, it is doing nothing.
@Hanya Idrees: What do you mean "it is doing nothing." That is saying very little. Is it throwing an error?
yea, dialog box is an jquery plugin i am using. And the same dialog box function works perfect on another page, called through a onclick event.
0

You are not returning any value in the showDialog() function.

Comments

0

Just get rid of document.write(), nothing is being returned by showDialog(), hence the undefined error.

<script type="text/javascript">  

  var myFile = document.getElementById('myfile');     
  //binds to onchange event of the input field     
  myFile.addEventListener('change', function() {     
  //this.files[0].size gets the size of your file.      
    var  size = this.files[0].size;      
    showDialog('size','File Size Exceeds');        
  });     

</script> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.