Jump to:

22997 Posts in 11844 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » jQuery function not defined

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 9470 Views
  • Aram
    Avatar
    Community Member
    598 Posts

    jQuery function not defined Link to this post

    Hi Guys

    I'm having a little trouble getting this jquery to run through SS. firebug reports that:

    slideSwitch is not defined
    [Break on this error] setInterval( "slideSwitch()", 500 );

    here is the code that I have in a file jquery.slide.show.js

    (function($){

          function slideSwitch() {
    //Some Stuff
          }
          
          $j(document).ready(function() {
           setInterval( "slideSwitch()", 500 );
          });
       
    })
    (jQuery);

    this works fine in a static HTML file, but as soon as I pass it through SS it breaks.

    Anyone know what I'm doing wrong?

    Cheers

  • banal
    Avatar
    Community Member
    901 Posts

    Re: jQuery function not defined Link to this post

    Hi aram

    Are you placing this in the jQuery "scope" on purpose? You're not extending $.fn or anything...
    I also wonder where that $j is coming from (see $j(document).ready in your code snippet)?

    I suggest you try something like this:

    var $j = jQuery.noConflict();

    var slideSwitch = function(){
       ... somestuff...
    };

    // the following does the same as $(document).ready
    $j(function(){
       window.setInterval(slideSwitch, 500);
    });

    Writing code like this has proven to work with SilverStripe (at least for me). And since you're not actually extending the jQuery functionality, there's no need to place your functions in the jQuery scope.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: jQuery function not defined Link to this post

    Thanks banal, that did the trick!

    I had misunderstood the use of the scope, I though that was needed for some reason.

    Thanks again

    9470 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.