Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sorry for a simple question.

I'm learning about plugins. I've got some javascript that I want to put in a jQuery file in my plugin.

(I've got jQuery and the scripts enqueued, and working fine).

Is it okay if I just prepend the javasvript to the jQuery?

So, the file would look like this:

//regular javasript
 function do_something() {
  //do Something
}

jQuery.noConflict();

jQuery(document).ready(function() {
  //my jQuery stuff
}

Is there a right way of doing this or does it not matter? If I put my straight javascript first (as above), it works.

If I put the javascript in with the jQuery, wrapping it in jQuery(document).ready(function() { }, then it doesn't.

Is it possible to add regular javascript in the jQuery ready function? Any ideas as to why it wouldn't work? I'm trying to learn.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

jQuery is regular javascript. It is a js library, meaning nothing but that it is a collection of js functions in an object. Hence, going with your terminology, yes, it can be mixed.

That being said, calling jQuery.noConflict(); is superfluous, since the library supplied by wordpress is loaded in noConflict mode anyway.

If something inside your jQuery(document).ready(function() {...} does not work as expected, there is most likely an issue with that code itsself.

You could edit that into your question and might get help here, but seeing that you have an account at stackoverflow as well, it might be a better idea to ask js/jQuery questions not specific to wordpress there.

To round this up: while you can declare nested functions in js, it would be good practice to declare them locally and thus keeping them out of the global namespace like so:

jQuery(document).ready(function() {
    var yourFunc = function() {
        // do something
    };
    yourFunc();
});
share|improve this answer
    
Thank you very much for the detailed answer. –  Laxmidi Nov 17 '11 at 5:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.