none
Referring jQuery and CSS in a Content editor web part in SharePoint

    Question

  • Hi All,

    I have a JS code which I will be using in a CEWP in any site in the SharePoint site collection. I'm trying to refer to jQuery plugin file which is in the Style Library in the root web of the Site Collection. I'm trying to refer it with the below code, but I'm getting an error:

        
    <script type="text/javascript" src="<% $SPUrl:~SiteCollection/Style Library/jquery.js%>"></script>



    Can anyone please tell me what is wrong in this. Thanks

    Regards,
    SC Vinod
    Blog
    : http://sharesilver.wordpress.com/



    • Edited by SC Vinod Wednesday, February 27, 2013 9:59 AM
    Thursday, November 08, 2012 11:51 AM

Answers

  • The path mentioned by Matthew in the below line of code

    <script src="/Style%20Library/jquery.js" type="text/javascript"></script>

    src="/Style%20Library/jquery.js" will be relative to the root site collection.

    I hope if you use src="/SiteAssets/jquery.js", will fulfill your requirement.

    Wednesday, November 14, 2012 12:44 PM

All replies

  • Try referencing the script like this:

    <script src="/Style%20Library/jquery.js" type="text/javascript"></script>


    Thursday, November 08, 2012 12:08 PM
  • Hello

    Have you tried this


    <script type="text/javascript" src="<% $SPUrl:~SiteCollection/Style%20Library/jquery.js %>"></script>

    replacing the space in the style library and adding a space between js and %>


    Best regards, Christopher.
    Blog | Mail
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.
    Why mark as answer?

    Thursday, November 08, 2012 12:10 PM
    Answerer
  • Hi Christopher, thanks for a suggestion. still not luck. I think this can be used only in a master page or or in an ASPX page and not in a Content editor web part.

    Can anyone please try and let me know. Thanks.


    Regards,
    SC Vinod
    Blog
    : http://sharesilver.wordpress.com/


    • Edited by SC Vinod Monday, November 12, 2012 4:11 PM
    Monday, November 12, 2012 4:06 PM
  • Hi SC Vinod,

    Did you try what I suggested? I tested this in Content Editor webpart and it works.

    Matt


    Regards, Matthew
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    Monday, November 12, 2012 4:15 PM
  • Matthew,

    Thanks for your suggestion. But my requirement is that I have the jQuery file uploaded in the Site Assets library in my top level site collection and I want to refer that jQuery file in a CEWP in any of the sub sites. So I want to get the URL of the top level site collection dynamically while referring the jQuery script. 


    Regards,
    SC Vinod
    Blog
    : http://sharesilver.wordpress.com/

    Tuesday, November 13, 2012 4:39 PM
  • The path mentioned by Matthew in the below line of code

    <script src="/Style%20Library/jquery.js" type="text/javascript"></script>

    src="/Style%20Library/jquery.js" will be relative to the root site collection.

    I hope if you use src="/SiteAssets/jquery.js", will fulfill your requirement.

    Wednesday, November 14, 2012 12:44 PM
  • this does not take you to the collection root.  The need for the SP variables in a script tag in a CEWP is something that I think MS did not think of.  I have not been able to accomplish it without using something like the following:

    
    var ssite = _spPageContextInfo.siteServerRelativeUrl;
    var script1 = document.createElement("script");
    		script1.setAttribute("type", "text/javascript");
    		script1.setAttribute("src", ssite + "siteassets/shared/js/jquery.SPServices-2013.01.min.js");
    		document.getElementsByTagName("head")[0].appendChild(script1);

    • Edited by jd_hancock Wednesday, October 02, 2013 10:57 AM
    Wednesday, October 02, 2013 10:56 AM