1

I have the following page:

<body>
  <div id="menuBar"> </div>
  <canvas id="myCanvas" width="700" height="700" style="border:1px solid #000000;"></canvas>
</body>

<script src="../Scripts/jQuery.mazeBoard.js"></script>
<script>
  $(document).ready(function () {
     var mazeArray = ...
     $("#menuBar").load("/Views/UpperMenu.html");
     $("#myCanvas").mazeBoard(mazeArray);
  })
</script>

mazeBoard is a jQuery plugin defined in mazeBoard.js:

(function ($) {

    $.fn.mazeBoard = function (mazeData) {

        this.element = $(this)[0];
        var context = this.element.getContext("2d");

        draw on canvas...

        return this;
    }
})(jQuery);

Strangely, unless I delete the row $("#menuBar").load("/Views/UpperMenu.html") I get a "mazeboard is not a function" error. This error disappears when the row is deleted.

5
  • unable to replicate, jsfiddle.net/v6exkas9 Commented Jun 16, 2017 at 12:04
  • Try to change jquery library another version.. it solves your problem.. Commented Jun 16, 2017 at 12:06
  • I get the same error. jonLuci, I get the error only when that line is not commented. Commented Jun 16, 2017 at 12:16
  • Simplest thing to try is to swap the order of the two lines; .mazeBoard() first, then .load(). Commented Jun 16, 2017 at 12:16
  • can you show us the contents of Views/UpperMenu.html ? you may have <script> tags which interfere Commented Jun 16, 2017 at 12:27

1 Answer 1

1

Hard to say why this error might occur but probably to do with .load() being asynchronous.

Simplest thing to try is to swap the order of the two lines :

$(document).ready(function () {
    var mazeArray = ...
    $("#myCanvas").mazeBoard(mazeArray);
    $("#menuBar").load("/Views/UpperMenu.html");
});

If that doesn't work, then you can await completion of .load() before initializing .mazeBoard() :

$(document).ready(function () {
    var mazeArray = ...;
    $("#menuBar").load("/Views/UpperMenu.html", function() {
        $("#myCanvas").mazeBoard(mazeArray);
    });
});
Sign up to request clarification or add additional context in comments.

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.