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.
我在用户控件的ASCX中有一些JS函数。此UC在主机页面中多次加载。当我查看视图源时,我看到JS重复了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 个解决方案
#1
Put the functions in a separate .js file, then add it by calling Page.ClientScript.RegisterClientScriptInclude().
将函数放在单独的.js文件中,然后通过调用Page.ClientScript.RegisterClientScriptInclude()添加它。
You can also mark the file as an Embedded Resource, and then include it using Page.ClientScript.RegisterClientScriptResource().
您还可以将文件标记为嵌入式资源,然后使用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.
请注意,如果您使用的是ScriptManager / UpdatePanels,请使用ScriptManager.RegisterClientScriptInclude或.RegisterClientScriptResource,以便它知道如何在部分回发上正确添加引用。
#1
Put the functions in a separate .js file, then add it by calling Page.ClientScript.RegisterClientScriptInclude().
将函数放在单独的.js文件中,然后通过调用Page.ClientScript.RegisterClientScriptInclude()添加它。
You can also mark the file as an Embedded Resource, and then include it using Page.ClientScript.RegisterClientScriptResource().
您还可以将文件标记为嵌入式资源,然后使用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.
请注意,如果您使用的是ScriptManager / UpdatePanels,请使用ScriptManager.RegisterClientScriptInclude或.RegisterClientScriptResource,以便它知道如何在部分回发上正确添加引用。