Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to call bootstrap modal window using javascript in chrome (works in firefox)

I have included the sample code from twitter-bootstrap for a modal window. If I click on the button it opens up the modal window. However if I try to show the modal window through javascript I get the following error -

$("#myModal").modal("show")

TypeError: Object [object Object] has no method 'modal'

This error is only coming in chrome and works in firefox.

Below is the HTML. "project-add-modal" is the id of the modal box. Navigate to Project -> Add to see the modal button. -

<html class=" js flexbox canvas canvastext no-webgl no-touch geolocation postmessage websqldatabase indexeddb hashchange history draganddrop websockets rgba hsla multiplebgs backgroundsize borderimage borderradius boxshadow textshadow opacity cssanimations csscolumns cssgradients cssreflections csstransforms no-csstransforms3d csstransitions fontface generatedcontent video audio localstorage sessionstorage webworkers applicationcache svg inlinesvg smil svgclippaths" lang="en"><!--<![endif]--><head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <title>Project Management</title>
    <meta name="description" content="">
    <meta name="author" content="">

    <meta name="viewport" content="width=device-width">

    <link rel="stylesheet" href="css/style.css">
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <link rel="stylesheet" href="css/bootstrap-responsive.min.css">

      <link rel="stylesheet" href="css/docs.css">

    <script src="//www.google-analytics.com/ga.js"></script><script src="js/libs/modernizr-2.5.3.min.js"></script>
</head>
<body class="">
<div id="header"><div id="top-navbar">
      <div class="navbar navbar-fixed-top">
        <div class="navbar-inner">
          <div class="container">
            <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </a>
            <div class="nav-collapse">
              <ul data-navlinks="left" class="nav">
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown">Projects<b class="caret"></b></a>
                  <ul class="dropdown-menu">
                    <li><a href="#" id="add-new-project" bind="click: pm.client.templating.hello">
                      Add
                    </a></li>
                  </ul>
                </li>
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown">Admin<b class="caret"></b></a>
                  <ul class="dropdown-menu">
                    <li><a href="#">Invite Users</a></li>
                  </ul>
                </li>
              </ul>
              <ul class="nav pull-right">
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown">Username<b class="caret"></b></a>
                  <ul class="dropdown-menu">
                    <li><a href="#">Logout</a></li>
                  </ul>
                </li>
              </ul>
            </div><!-- /.nav-collapse -->
          </div>
        </div><!-- /navbar-inner -->
      </div>
    </div></div>
<div class="container cljs-main"><div id="project-add-section">
<a class="btn" data-toggle="modal" href="#project-add-modal">Launch Modal</a>

      <div class="modal hide fade" id="project-add-modal" style="display: none; ">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">×</button>
          <h3>Add New Project</h3>
        </div>
        <div class="modal-body">
          <form class="form-horizontal">
            <fieldset>
              <div class="control-group">
                <label class="control-label" for="input01">Project Name</label>
                <div class="controls">
                  <input type="text" class="input-xlarge" id="project-name">
                </div>
              </div>
              <div class="control-group">
                <label class="control-label" for="textarea">Project Description</label>
                <div class="controls">
                  <textarea class="input-xlarge" id="project-desc" rows="3"></textarea>
                </div>
              </div>
              <label class="control-label">Invite People to
              collaborate on your project</label>
              <div class="control-group">
                <label class="control-label" for="prependedInput"></label>
                <div class="controls">
                  <div class="input-prepend">
                    <span class="add-on">@</span><input class="span2" id="prependedInput" size="16" type="text">
                  </div>
                  <p class="help-block">Here's some help text</p>
                </div>
              </div>
            </fieldset>
        </form></div>
        <div class="modal-footer">
          <a href="#" class="btn" data-dismiss="modal">Close</a>
          <a href="#" class="btn btn-primary">Save changes</a>
        </div>
      </div>

    </div></div>
<footer>

</footer>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.2.min.js"><\/script>')</script>

<!-- scripts concatenated and minified via ant build script-->
<script src="js/bootstrap.min.js"></script>
<script src="js/plugins.js"></script>
<script src="js/script.js"></script>
<script src="cljs/client.js"></script><script type="text/javascript" src="deps.js"></script>
<script>$(function() {cljsbinding.boot()})</script>
<!-- end scripts-->


<script>
    var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];
    (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
    g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
    s.parentNode.insertBefore(g,s)}(document,'script'));
</script>




<iframe name="xpcpeer7X7y" id="xpcpeer7X7y" style="display: none; " src="http://localhost:9000/repl?xpc=%7B%22cn%22%3A%22PmCIncJdVC%22%2C%22tp%22%3Anull%7D"></iframe></body></html>

Thanks, Murtaza

like image 215
murtaza52 Avatar asked Jun 27 '12 06:06

murtaza52


1 Answers

I noticed this problem may occur under the following conditions:

  • JQuery has been loaded twice.
  • The individual twitter bootstrap plugin is loaded on top of bootstrap.js
  • The .js files are included in the wrong order
  • The wrong type is sent to the function.
like image 77
baptme Avatar answered Oct 19 '22 03:10

baptme