0

I have a file- "Site.js" with the following method:

function Init() {
  $(document).ready(function() {
    //some init actions here
  });
}
function jQuery.fn.DivToggle(div) {
  $(div).toggle('fast');
  //some other animation code here
}

in my Index.cshtml file (I'm using asp.net mvc), I have this:

<!DOCTYPE html>
<html>
  <head id="Head1" runat="server">
    <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Site.js")" type="text/javascript"></script>
  </head>
  <body>
    <script type="text/javascript">
      $(function () {
        Init();
      });
    </script>
  </body>
</html>

I get this error when I run the project: "Microsoft JScript runtime error: 'Init' is undefined"

Any idea what I'm doing wrong?

6
  • 1
    do you have all of the functions in Site.js wrapped in a $(function () { all functions here }); tag? if so, the init() function will be private an unable to call it from outside of Site.js Commented Apr 13, 2012 at 18:11
  • why document.ready is inside Init() function? Commented Apr 13, 2012 at 18:13
  • @JohnnyCraig: Nope. Just: function Init() { //code } Commented Apr 13, 2012 at 18:20
  • @DotNetter Removed document.ready, still see the same issue. Commented Apr 13, 2012 at 18:21
  • 1
    function jQuery.fn.DivToggle(){} <= you can't actually do that. It should be jQuery.fn.DivToggle=function(){} Commented Apr 13, 2012 at 18:30

3 Answers 3

3

Init is not defined because there is a syntax error in Site.js.

function jQuery.fn.DivToggle(div) {
  $(div).toggle('fast');
  //some other animation code here
}

You can't actually do that. It should be:

jQuery.fn.DivToggle = function(div) {
  $(div).toggle('fast');
  //some other animation code here
};

P.S. $(function(){ is the same as $(document).ready(function(){. You don't need the $(document).ready(function(){ inside your Init function.

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

Comments

1

I think the real problem is with your DivToggle() function. You should change that code to the following:

(function( $ ) {
  $.fn.DivToggle = function() {
     $(this).toggle('fast');
  };
})( jQuery );

Then you can call DivToggle by creating a selector and calling it. So if you want to run this on all of the div tags in your page do this:

$('div').DivToggle();

Here is some documentation on creating custom plugins: http://docs.jquery.com/Plugins/Authoring

You probably need to change the way you are calling Init() and document.ready(). Actually the Init() function isn't really doing anything right now. I think you can change your code to this and it would accomplish what you want:

<script type="text/javascript">

//create the DivToggle() function
(function( $ ) {
  $.fn.DivToggle = function() {
     $(this).toggle('fast');
  };
})( jQuery );

$(function () {
    //call DivToggle() on all <div> tags when the page loads
    $('div').DivToggle();
});

</script>

Comments

0

Try namespacing your javascript:

 StackOverflow = {

      function Init(){

      }

      return {
        Init: Init
      };
 }();

Call your function with

$(function(){
     StackOverflow.Init();
});

4 Comments

Why would this make a difference?
@Rocket I think it is a good practice to namespace your JavaScript.
Shouldn't it be StackOverflow = (function(){...}());?
It may be a good idea, but I don't see how it relates to the question.

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.