Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint Custom JS file Best Practice

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?

like image 397
bmw0128 Avatar asked Mar 18 '10 17:03

bmw0128


People also ask

Can you use JavaScript in SharePoint?

You can update your SharePoint site's UI by using JavaScript. This extensibility option is only available for classic SharePoint experiences.

Does SharePoint use CSS?

Cascading style sheet (CSS) plays a large role in SharePoint branding. To successfully customize the site design in SharePoint and SharePoint Online, it's useful to be familiar with how SharePoint uses CSS. This extensibility option is only available for classic SharePoint experiences.


2 Answers

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" />
like image 126
Jay Avatar answered Sep 28 '22 04:09

Jay


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>
like image 33
Rich Bennema Avatar answered Sep 28 '22 06:09

Rich Bennema