views:

58

answers:

1

I am working on expansion of my jQuery plug-in authoring knowledge, just playing around with my own ideas for learning benefits. So i was wondering how you guys tackle the need of plugin executing without any user specific input.

So I have a need for plug-in that executes right away after document is ready, without any user's specific input just as long as doc is loaded, so the only way I see how to execute plug-in on it's own is to attach handler to ready listener that executes my function which I extended the jQuery with. And because this needs to be self enclosed, part of the code, I attach handler to event listener within the function.

So how else could one tackle this? Any take at this is appreciated. Thank you in advance everyone.

A: 

Just do

$(document).ready(function() {
     function_that_starts_your_plugin();
});

within the .js file that contains your plugin. As long as the appropriate <script> tag loads the .js file, then your code will get started automatically. If you want the option to change options on a per-page basis, or disable completely, put some sentinel variables in another javascript block before the one that loads your plugin:

<script type="text/javascript"> var mypluginAutoRun = FALSE;</script>
<script type="text/javascript" src="plugin.js"></script>
Marc B
thank you Marc B, no pun intended, but that's exactly how I do that right now. I am not sure what exactly pushed me to post this topic. Probably hoping in turn to learn of other ways.
GnrlBzik