How to check if the client script Resource is registered on the page?

I developed a custom control (not a user control) that uses some jQuery scripts.

I included the script control as a built-in resource in the assembly and registered it on the page in the OnPreRender event, for example:

protected override void OnPreRender(EventArgs e)
{
   string controlScript = "Namespace.MyScript.js";
   ClientScriptManager csm = this.Page.ClientScript;
   csm.RegisterClientScriptResource(typeof(ResourceFinder), controlScript );
   base.OnPreRender(e);
}

This control works when deployed to a project with an existing jQuery library, but what if the jQuery library is not available. How can I check if the library is already in the project, and if not, then build it into the assembly and pull it out. I thought something like:

protected override void OnPreRender(EventArgs e)
{
    ClientScriptManager csm = this.Page.ClientScript;

    // Here I would need to check if jQuery is already registered
    // but there is no such method like: IsClientScriptResourceRegistered
    if (csm.IsClientScriptResourceRegistered("jQuery"))
    {
        string jQueryLib = "Namespace.jquery-1.7.2.min.js";
        csm.RegisterClientScriptResource(typeof(ResourceFinder), jQueryLib);
    }

    string controlScript = "Namespace.MyScript.js";
    csm.RegisterClientScriptResource(typeof(ResourceFinder), controlScript );
    base.OnPreRender(e);
}

I know that in any case, I could embed the jQuery library in the assembly, but if jQuery was already in the project, I would get a page with two libray calls, for example:

<!-- Project jQuery lib -->
<script type="text/javascript" src="/scripts/jquery-1.7.2.min.js"></script>
<!-- DLL duplicated jQuery lib:  -->
<script type="text/javascript" src="/WebResource.axd?d=AP7y...r0bHQl"></script>
<!-- DLL Control MyScript.js -->
<script type="text/javascript" src="/WebResource.axd?d=uTGR...ur15b"></script>

... this is what I am trying to avoid.

?

+3
1

script, jQuery . jQuery , .

<script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.1.min.js"><\/script>')</script>
+1

All Articles