0

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

No the JS that uses jQuery is the embedded resource, but currently when the control is rendered to the page, I get a "'$' is not defined" JavaScript error on the embedded script's first use of the $ alias
Then jQuery is being loaded after the control, or not at all. Is the error happening on page load or during a user action later on?
jQuery is a ScriptReference of the ScriptManager on the page's MasterPage. And the error happens on page load, it's occurring on the " $(document).ready(function() { " from in the embedded script.
Well you could have any number of issues but it appears as though jQuery is not loaded by the time the control is firing its client side code. You could insert a check to see if jQuery is loaded like this if (jQuery) { // it loaded ok }. I assume if you take the control off the page you don't have any other errors?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.