Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object doesn't support property or method 'dialog'

Refering the AjaxControlToolkit, I created a UI dialog from MVC.

Layout.cshtml

 <head>
   <meta charset="utf-8" />
   <title>@ViewBag.Title - My ASP.NET MVC Application</title>
   <link href="../../Content/smoothness/jquery-ui-1.10.2.custom.css" rel="stylesheet" type="text/css" />
   <script src="../../Scripts/jquery-1.9.1.js" type="text/javascript"></script>
   <script src="../../Scripts/jquery-ui-1.10.2.custom.min.js" type="text/javascript"></script>
   <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
   <meta name="viewport" content="width=device-width" />
   @Styles.Render("~/Content/css")
   @Scripts.Render("~/bundles/modernizr")
</head>

Index.cshtml

<h2>jQuery UI Hello World</h2>          
<button id="show-dialog">Click to see jQuery UI in Action</button>            
<div id="dialog" title="jQuery UI in ASP.NET MVC" >  
  <p>You now have the power of ASP.NET, the simplicity of client-side scripting with jQuery, and the looks of jQuery UI. Congrats, web slinger!</p>  
</div>  

<script language="javascript" type="text/javascript">
  $(function () {
    $('#dialog').dialog({
      autoOpen: false,
      width: 600,
      buttons: {
        "Ok": function () { $(this).dialog("close"); },
        "Cancel": function () { $(this).dialog("close"); }
      }
    });
    $("#show-dialog").button().click(function () {
        $('#dialog').dialog('open');
        return false;
    });
  });  
</script>    

I checked both in IE and Firefox. Firefox throws

Typeerror $(...).dialog is not a function

IE throws

Object doesn't support property or method 'dialog'

Any suggestions?

like image 710
kk1076 Avatar asked Mar 28 '13 10:03

kk1076


3 Answers

3 things come to mind that might be worth checking:

  1. Never hardcode urls in an ASP.NET MVC application. Always use helpers (or bundles which are recommended):

    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/Content/smoothness/jquery-ui-1.10.2.custom.css" rel="stylesheet"  type="text/css" />
        <script src="~/Scripts/jquery-1.9.1.js" type="text/javascript"></script>
        <script src="~/Scripts/jquery-ui-1.10.2.custom.min.js" type="text/javascript"></script>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    </head>
    
  2. Make sure that at the end of your _Layout.cshtml you don't have a @Scripts.Render("~/bundles/jquery") call because this would include jQuery twice.

  3. If at the end of your _Layout.cshtml you have a dedicated section for custom scripts like @RenderSection("scripts", required: false), make sure that you have placed your custom script in that section (notice that since this RenderSection is at the end of the DOM you don't need to be wrapping your script in a document.ready event because by the time it executes, the DOM will already be loaded):

    <h2>jQuery UI Hello World</h2>          
    <button id="show-dialog">Click to see jQuery UI in Action</button>            
    <div id="dialog" title="jQuery UI in ASP.NET MVC" >  
       <p>You now have the power of ASP.NET, the simplicity of client-side scripting with jQuery, and the looks of jQuery UI. Congrats, web slinger!</p>  
    </div>    
    
    @section scripts {
        <script type="text/javascript">
            $('#dialog').dialog({
                autoOpen: false,
                width: 600,
                buttons: {
                    "Ok": function () { $(this).dialog("close"); },
                    "Cancel": function () { $(this).dialog("close"); }
                }
            });
            $("#show-dialog").button().click(function () {
                $('#dialog').dialog('open');
                return false;
            });
        });  
        </script>
    }
    
like image 116
Darin Dimitrov Avatar answered Nov 11 '22 14:11

Darin Dimitrov


In my case, this error was because I had forgotten to add the jquery-ui file reference:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js" type="text/javascript"></script>
like image 21
live-love Avatar answered Nov 11 '22 16:11

live-love


This commonly happens when you forget to add jquery-ui.js. The order of including jquery-ui-{version}.js also matters!

You should include jquery-{version}.js then jquery-ui-{version}.js. Then just before </body> tag, include your custom javascript file.

It will solve Javascript runtime error: [Object doesn't support property or method 'dialog'],['$' is undefined]

like image 3
DevelopZen Avatar answered Nov 11 '22 16:11

DevelopZen