OK... seeing as no one seems to know and digging around Google for hours on end didn't show up anything useful, for anyone that stumbles across this post this is what I did, it may not be relevant to you but if it is I hope it helps.
It doesn't seem that I can only call the javascript when the widget is being used on that page so I had to come up with a different approach. My plugin used PHP to place a tag in sidebar like this...
<div id="gallerywidget"></div>
The javascript (jQuery based) would then search for the tags and put the relevant HTML in there. Regardless of what page it was or if the tags were there the function would be called and it would retrieve Flickr data and the images but not use them... stupid and pointless overhead for any page not using it.
Simply what I did to remedy it, and this is not perfect but a lot better then it was, was to wrap the innards of the main function that was being called like this...
function populategallery(){
if ( jQuery("#gallerywidget").length > 0 ) {
// FUNCTION CODE
}
}
This meant when the widget wasn't being called, the div tags with the ID weren't being output and so the function effectively would do nothing. It's a vast improvement as I say and I'll be happy to release this plugin using this method but I'll be keeping an eye out for a better way in the future.