How to avoid module specific jQuery plugin script running globally? This is my first time working with Drupal (Drupal 7).

I've created a jQuery plugin which populates a target div. The target div is added to the node via panelizer.

I only want to run the script for when it's needed. Just now it runs all the time. Even on the dashboard, this causes script errors. 

I'm not sure what the best approach is for this.

EDIT (SOLUTION): 

Thanks for the input. I decided to use a Javascript approach. 

var elementExists = document.getElementById('target');

if (typeof(elementExists) != 'undefined' &amp;&amp; elementExists != null) {
  // Script
} else {
    return;
}
From my experience it's usually best to leave this check to JS and don't bother on PHP side. Of course it all depends on your definition of best, but benefits I see are:


It's easy and stable to test for div existence with $("#myid"), opposed to more mysterious tests in PHP. Now you know that this div will appear when X is true and Y == Z. Will you remember that in two months?
Sure, we have comments. But will future developer understand what you meant? Will you be disciplined enough to write them, and do it really clearly?
It's easier for caching - if you aggregate your scripts, you will use the same aggregated file for pages with and without div, so one less check / download for browser
It lessens the chance for conflict between your module and other ones. 


Modules shouldn't just overwrite [#attached] array, but I encountered some that did.
If you are using drupal_add_js instead, making sure test for conditions is run when it is supposed to might be hard, if other modules will try to reorder hooks. So this isn't a sure way to avoid problem described above.
Various caching systems might miss the conditions you use. Probably nothing as severe as Boost and Mobile Tools issue, but risk is there.
Theme shouldn't need to know about blocks - if you will leave test to theme, point above will not apply, but you risk crashes whenever theme is switched or block is disabled. Testing for it on both sides will add a lot of code.
And I'm sure there are other things I simply never encountered yet.


Of course you could say most of those are bugs in other modules, and you would be right, but being right does not help if you need your site up and running.
It's easier to quickly see scripts added via .info file than to search code for drupal_add_js or attached or... you see, there are many ways to do it in code, happy hunting if you are new developer and need to find where this one script came from.