0

I have some JS functions within the ASCX of a user control. This UC is loaded multiple times in the host page. When I do a view source, I see the JS repeated for every instance of the UC.

Is there a way I can have it load the functions only once? I would like to reduce the size of the page for better performance

1 Answer 1

2

Put the functions in a separate .js file, then add it by calling Page.ClientScript.RegisterClientScriptInclude().

You can also mark the file as an Embedded Resource, and then include it using Page.ClientScript.RegisterClientScriptResource().

Note that if you're using a ScriptManager/UpdatePanels, use ScriptManager.RegisterClientScriptInclude or .RegisterClientScriptResource so that it'll know how to add the reference correctly on a partial postback.

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

1 Comment

+1 because it was better than my answer - prevents multiple loads of js

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.