Search code examples
jquerycustom-controlsembedded-resourceweb-controlswebresource.axd

Control's embedded JavaScript using jQuery


I am creating a custom WebControl in my class library that has an embedded resource JavaScript file that it utilizes. That JavaScript uses jQuery.

My question is, if the ScriptManager on the page that the control is to be used on, is guaranteed to have a ScriptReference to jQuery, do I still need to have jQuery as an embedded resource in the class library for the control's JavaScript to be able to make use jQuery?

If the answer is yes, what happens when I create other controls that also have embedded JavaScript that use jQuery and a few different of these controls are used on the same page? Is the embedded jQuery script added to the page as a webResource multiple times?


Solution

  • Is jQuery the embedded resource or some JS that uses jQuery?

    Either way, you only need one reference to jQuery in your ScriptReferences. If jQuery itself is embedded in the control (terrible design) then you should remove it if possible.

    Loading jQuery once at the top of the page has many advantages, however the first and foremost is the fact that other areas of the page might require it so relying on it to be loaded within a control is a terrible idea.

    In summary, do whatever you can to not have jQuery (or any script resources) loaded multiple times on the same page