SharePoint Custom JS file Best Practice
Asked Answered
R

4

8

I made a custom master page. I also made a custom CSS file, which I uploaded to "Style Library". I link to this CSS file by:

<SharePoint:CssRegistration name="<% $SPUrl:~SiteCollection/Style Library/coe/mainCOE.css%>" runat="server"/>

Now I want to make a custom JS file, and link to it in the custom master page. 1. Where should I upload the JS file, make a folder like the "Style Library"? 2. Is there a SharePoint way to link to the JS file, like the one above for a CSS file?

Reneerenegade answered 18/3, 2010 at 17:59 Comment(0)
C
0

You can upload that js file in site collection document library, if you want to get this to all site collections then please create a folder in side layouts and paste it there. And refer this is in your custome master page.

Canute answered 19/3, 2010 at 8:12 Comment(0)
W
20

You can store JS files in the _layouts folder, but storing them in the Style Library gives you workflow/change history for free.

If you keep your JS files in the Style Library you can reference them in your MasterPage using the ~sitecollection token by using the <SharePoint:Scriptlink> tag, like so:

<SharePoint:Scriptlink runat="server" Name="~sitecollection/Style Library/[YOUR SITE]/js/functions.js" Language="javascript" />
Waybill answered 18/8, 2010 at 8:8 Comment(1)
This should be the accepted answer, especially nowadays. _layouts is so 2010 :PMonkey
S
5

Use Style Library and use CssRegistration and ScriptLink if they work. But sometimes SharePoint puts the links in an order you don't want. In that case, you can use the following to directly insert the references:

<link rel="stylesheet" type="text/css" href="<% $SPUrl:~SiteCollection/Style Library/coe/mainCOE.css%>" runat="server"/>
<script type="text/javascript" language="javascript" src='<asp:Literal runat="server" Text="<%$SPUrl:~SiteCollection/Style Library/coe/mainCOE.js%>" />'></script>
Selffertilization answered 19/3, 2010 at 16:38 Comment(0)
G
0

Check out the SharePoint:ScriptLink class, which is similar to the SharePoint:CssRegistration.

The documentation is pretty sparse, but try this http://www.go4sharepoint.com/Forum/sharepointscriptlink-6462.aspx

Goldberg answered 19/3, 2010 at 3:1 Comment(0)
C
0

You can upload that js file in site collection document library, if you want to get this to all site collections then please create a folder in side layouts and paste it there. And refer this is in your custome master page.

Canute answered 19/3, 2010 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.