> I've come to a situation of making one .js file for many pages.
>
> So, some pages have certain divs and some don't.
>
> My situation is that whenever a plugin was defined for a certain div
> in a certain page,
>
> I seem to see it being invoked even on pages that don't have that div.
>
> For example if page.html has a div called : <div id="page"><div
> class="a"></div></div>
>
> In a .js file I call: $('#page .a').plugin(); (let's say it does
> alert('hi'); )
>
> But on page2.html, I would see that plugin get invoked anyways, and a
> message box would say 'hi'.
> page2.html doesn't have <div id="page">. It only has <div id="page2">
>
> This causes a problem because I have to constantly manually check if
> all components needed for a plugin
> are there on the page, which I think the selector - plugin call line
> should automatically handle.
>
> Can anyone help me, or has anyone had the same situation?
>
> Thanks,
> Seungjin Kim


The plugin function is still invoked, it just has no DOM elements on
which to operate.  So if you have a plugin like this:

jQuery.fn.myPlugin = function() {
    alert ( 'hello world' );
    return this;
};

You will see an alert regardless of the selection criteria.  But if
you do this it will not alert:

jQuery.fn.myPlugin = function() {
    if (size() > 0)
        alert ( 'hello world' );
    return this;
};

"Typical" plugins, though certainly not all, operate on DOM elements
and use iteration to mask this caveat:

jQuery.fn.myPlugin = function() {
    return this.each(function() {
        // operate on DOM element here
    });
};

Hope this helps.

Mike

Reply via email to