Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a webpart that requires jQuery to run. Therefore, in my code I want to check if jQuery is not on the page inject it. I've got down the inject part, but the detection part it where I'm struggling. I tried to the following two things:

  1. Register script using ScriptLink.Register(). I expected better from this method, but all it did was put inject the script, without even checking if the script exists. So, I registered jQuery in the master page using ScriptLink and then called ScriptLink.Register() to register same script (same name, same path), and SharePoint still registered it twice.
  2. Then I created a loop to check all controls on the page and check if its a ScriptLink, and then check if it had jQuery. If no scriptlink control had jQuery, then I injected it. I expected this to work, but instead the no jQuery was loaded on the page. This was super-weird.

I'm still diagnosing the issue, but wanted some advice. Maybe there is some other (better) way to accomplish this.

So this is all I want to do: Check if there is jQuery (or any script) registered on the page. If not, then registered it.

I can possibly also use JavaScript, but all my JS files are embedded resources. So, those cannot be loaded using client side code. WebParts don't support WebMethods, so that's a bummer too.

Many thanks for reading this never-ending question and giving a thought to my problem. Much appreciated!

UPDATE: The code I'm using for the loop in step 2. (*Note, as of now it's only checking the page header controls, but it'll probably be the whole page, which will also kill performance) =(

// The code below is bad, but it's a quick write-up, please don't scold me for that. =(
// parse header controls
            foreach (var itm in page.Header.Controls)
            {
                if (itm is ScriptLink)
                {
                    if ((itm as ScriptLink).GetDesignTimeHtml().Contains(keyword) || (itm as ScriptLink).GetDesignTimeHtml().Contains(keyword))
                    {
                        containsScript = true;
                        break;
                    }
                }
            }
share|improve this question

4 Answers 4

up vote 2 down vote accepted

This blogpost might have what you are looking for: http://ateraprime.com/community/Blog/Lists/Posts/Post.aspx?ID=22

He is going through the header controls and checking against the name attribute in the ScriptLink control. You can view the code on the bottom of the post. I'm not sure if I can post it here.

Hopefully that answers your question.

share|improve this answer
    
Cool post. It only checks against header controls, but I can code it to check the entire body. Thanks for the help! Much appreciated! –  Umm.... May 22 '12 at 19:16

If you control all pieces of code that could potentially add Javascript to the page then you can use the Page.ClientScript.RegisterClientScriptInclude method:

Page.ClientScript.RegisterClientScriptInclude("jquery", "/myPath/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 include it somewhere else using a different name then you will get weirdness.

From the client side you can also do something like this:

<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>

Hope this helps!

share|improve this answer
    
Yeh, that is the behavior I am expecting. I do not have control over the master page and the scripts in there. Since my javascript files are embedded resources, I cannot use client side code. Thanks for the help! –  Umm.... May 22 '12 at 20:06

You might be looking for the ClientScriptManager.RegisterClientScriptInclude method instead.

share|improve this answer
    
I've looked into it, but from what I know, it only checks against the key you provide. So, it will still register the jQuery file on the page, even though there is a jQuery file registered using ScriptLink. =( I just ran this code, with failure. –  Umm.... May 22 '12 at 18:01

If the masterpage includes a ScriptLink and a web part call ScriptLink.Register on the same js file then the file should only be included once. Make sure you got the same parameters.

May not work with embedded resources, but I've just tested it with .js file in _layouts

In my masterpage I included:

<SharePoint:ScriptLink language="javascript" name="ScriptLinks/JScript1.js" runat="server" Localizable="FALSE"/>
<SharePoint:ScriptLink language="javascript" name="ScriptLinks/JScript1.js" runat="server" Localizable="FALSE"/>

My web part contains this:

protected override void CreateChildControls()
{
    ScriptLink.Register(Page, "ScriptLinks/JScript1.js", false);
}

And my view source contains only this once:

document.write('<script type="text/javascript" src="/_layouts/scriptlinks/jscript1.js?rev=oKYRWs42wvGtYO%2F%2BFj7meg%3D%3D"></' + 'script>');
share|improve this answer
    
Interesting....I'm referencing same file in same folder. The only difference I see is that when I see the page source in the browser, my registered script gets type="text/javascript" and the one in the master page does not get the type attribute. I doubt that's the issue, but its SharePoint, you never know. –  Umm.... May 22 '12 at 18:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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