1

How to make a jQuery plugin which needs some methods but doesn't need a selector like the following example?

$.plugin.method(param);
Milad Rahimi
  • 3,464
  • 3
  • 27
  • 39
  • So for what pupose you want to develop a plugin, when you don't need a selector. You need a selector to apply your plugin. – Jai Mar 05 '15 at 11:07
  • Cause I use JQuery and its methods, so it's a jQuery plugin, In my opinion every jQuery plugin should be name jquery.plugin.js and uses $. – Milad Rahimi Mar 05 '15 at 11:11
  • Okay! but yet you need to refer it with `this` keyword in this case. – Jai Mar 05 '15 at 11:13
  • that's my problem, it's easy to make $.plugin(); but about $.plugin.method(), mmm... – Milad Rahimi Mar 05 '15 at 11:15
  • see this link: http://stackoverflow.com/a/210733/1059101 shortest jquery plugin to center an element to the page. – Jai Mar 05 '15 at 11:16
  • It's in $(selector).plugin(); format, different from what I need. – Milad Rahimi Mar 05 '15 at 11:22

1 Answers1

3

Finally found the answer,

// jQuery Plugin Syntax
(function ($) {
    $.plugin = {
        method: function(param) {
            alert(param);
        };
    };
}(jQuery));

Now you can call it this way:

$.plugin.method("Hello World!");
Milad Rahimi
  • 3,464
  • 3
  • 27
  • 39