1

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.

1 Answer 1

4

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();
});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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