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

Let me explain the situation:

I have a Sharepoint 2010 solution in VS that contains:

  • Some Model classes
  • A LINQ-to-SQL data context
  • A WCF REST service
  • 3 Visual web parts

The web parts calls some methods in the WCF service using jQuery.

Right now, I'm adding the jQuery script from Microsoft Ajax CDN directly in the Visual Web Part markup (.ascx).

<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>

The thing is that if more than 1 web part is inserted in the same page, that script is being added more than once.

How can I make things so the script is registered only once, no matter how many web parts are inserted in the same page?

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

you can also use CustomAction for loading jquery in page.

just create new element file and add custom action tab like that

   <CustomAction
   ScriptSrc="~site/_layouts/yourjquerypath/jquery.js"
   Location="ScriptLink"
   Sequence="100"/>

so do not need to add jquery pugin every time in webparts.

share|improve this answer
add comment

You could use an usercontrol witch you load into the "AdditionalPageHead" section. This will allways include your script, wether or not one (or more) of your web parts is on the page. I used this technique for displaying custom ribbons, and you can find the sourcecode at http://swcustomribbon.codeplex.com/

share|improve this answer
    
How exactly do I do that? I want the jquery script to be added when my solution is deployed, or maybe when a feature is activated, even if the webparts are not yet inserted in any page. Is that possible? –  emzero Mar 31 '12 at 2:03
    
there you find a step by step tutorial: sharepointjohn.com/… –  Stefan Mar 31 '12 at 5:41
add comment

You don't mention whether you are in the sandbox or a full-trust solution.

For full-trust, you should use the ASP.NET facility for registering scripts: ClientScriptManager

If you follow the pattern in the example on that page, the script will get registered only a single time, and downloaded a single time.

The ClientScriptManager is not available in the sandbox. (Last time I tried, it did not throw any errors, it simply does not work.) As an alternative, the CustomAction/ScriptLink approach would be fine.

share|improve this answer
    
Fulltrust. I'll try this. Thanks. –  emzero Mar 31 '12 at 17:51
add comment

Like @V_B said, it is better to load jquery as customaction to all pages, another way is to define scripts in your masterpage. But if that doesn't suit you, you can add jQuery in your webparts conditionally if the script has been loaded or not like described in CSS tricks.

if (typeof jQuery === undefined) {
  //load jQuery
}
//the rest of code

UPDATE: See an example in my gist:

share|improve this answer
add comment

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.