1

By example we have two webparts on page, with ascx controls.

One ASCX control loads

                this.Controls.Add(new ScriptLink()
                {
                    Name = "/layouts/15/Webpart1Control/jQuery.js",
                    Language = "javascript",
                    Localizable = false
                });

and the other ASCX control from different webpart do

                this.Controls.Add(new ScriptLink()
                {
                    Name = "/layouts/15/Webpart2SecondControl/jQuery.js ",
                    Language = "javascript",
                    Localizable = false
                });

What will be in this case and how to do correct?

1 Answer 1

2

You could use the something like

const string scriptKey = "jQuery";

if( !Page.ClientScript.IsClientScriptIncludeRegistered( Page.GetType(), scriptKey ) )
{
    Page.ClientScript.RegisterClientScriptInclude( Page.GetType(), scriptKey, ResolveClientUrl("~/js/jquery.min.js" ) );
}

The key to making this work is that you need to include the same key anywhere you include jQuery. This method checks for a script with an existing key, and if none exists it adds the script.

If you want to achieve the same result on the client side you can also do the following:

<script>
if(!window.jQuery)
{
   var script = document.createElement('script');
   script.type = "text/javascript";
   script.src = "path/to/jQuery";
   document.getElementsByTagName('head')[0].appendChild(script);
}
</script>
1
  • thank You for reply! In this case I can control only my code, but what if somebody else already added same file? Maybe there is some way to check if file is already loaded in memory..
    – Gennady G
    Commented Feb 15, 2015 at 21:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.