0

I have two web parts that use jquery on the same page. this causes one of them not to work. One of them is an actual web part with a reference to ~SiteCollection/Style Library/NewsTicker/Scripts/jquery.js in it. this is a list scrolling webpart. The other webpart s a content webpart that gets the weather. it references jquery like this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

What is causing these two to conflict. the weather web part works but the scroller does not. this is for SharePoint 2013

1

You are loading two different jquery files with different file names (means both will be loaded by the browser). The one jquery will override the other on runtime.

I think while the first jQuery is waiting for the DOM to be ready, it will be overwritten by the second jQuery.

=> so the $(document).ready of one of the webparts will never be called because the "old" jQuery object does not exist anymore.

If you have full control of the webpart's code, then don't use $(document).ready!

Code of Weather Webpart:

function InitializeWeatherWebpart() {
    /* Code, which would be in the callback function of $(document).ready */    
}

// Add the following methods the the list of functions being called when DOM is ready
_spBodyOnLoadFunctionNames.push('InitializeWeatherWebpart');

Code of Scroller Webpart:

function InitializeScrollerWebpartBtwImUnique_MaybeWithAGuid() {
    /* Code, which would be in the callback function of $(document).ready */    
}

// Add the following methods the the list of functions being called when DOM is ready
_spBodyOnLoadFunctionNames.push('InitializeScrollerWebpartBtwImUnique_MaybeWithAGuid');

If you do it so, you are not dependent of a jQuery waiting for a ready DOM. SharePoint can do it also very good.

0

You are loading 2 instances of jQuery and that is what is breaking.

I would either include the jQuery reference on your master page or in your page layout. Then your javascript for your web parts don't need to load it.

  • There seems to be no straightforward way to do this. any references would help – rigamonk Oct 8 '14 at 15:50
  • When I add the reference to the master page and remove it from the content editor it no longer works:<script src="../../Scripts/1.7.2.jquery.min.js" type="text/javascript"></script> – rigamonk Oct 8 '14 at 16:14
  • You can safely load two (or more) different versions of jQuery. You can alias the different versions and use proper scoping to use the correct version as necessary. Please see the noConflict() documentation. – nsturdivant Oct 10 '14 at 16:01
0

Putting the jQuery script tag in the master page should work. Try a complete hard coded URL of the script file to be sure there is not a problem with the relative address parsing. So something like src="http://........./Style Library/NewsTicker/Scripts/jquery.js" (replace the "........." with the appropriate site collection and site path)

Then don't forget to remove the script tag from your webparts!

But no matter what, one way to avoid loading the jQuery library twice is to include this in your script:

if(typeof jQuery=='undefined'){
   var jQueryLib = 'http://........./Style Library/NewsTicker/Scripts/jquery.js'; 
   document.write('<script src="',jQueryLib,'" type="text/javascript"><\/script>'); 
}

The jQuery library will only be loaded if it is not found already.

The _spBodyOnLoadFunctionNames suggestion is a good standard to use in SharePoint instead of the $(document).ready

You can find lots of examples of that by googling it

0

Try generating relative URL for you js in your masterpage.

<SharePoint:ScriptLink Language="JavaScript" Name="~Site/SiteAssets/File.js" runat="server" OnDemand="false"></SharePoint:ScriptLink>

Or

<script type="text/javascript" src='<asp:Literal runat="server" Text="<%$SPUrl:~Site/%>" />SiteAssets/File.js'></script>

Check out following link:

http://customsp.blogspot.com/2014/10/relative-path-in-sharepoint.html

  • We do not allow link only answers with the sole purpose to promote your private blog. Please add details to your answer here, and at most use the link to your blog as a reference only – Robert Lindgren Oct 21 '14 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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