2

This is probably a real NOOB question but I've been struggling to link a source file into my page code. I want to move the cookie code out of the page code.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="/SiteAssets/cookies.js" type="text/javascript"></script>


<script type="text/javascript">

// sets the cookie value and uses "/" path
function setCookie(cname, cvalue, exdays) {
   var d = new Date();
   d.setTime(d.getTime() + (exdays*24*60*60*1000));
   var expires = "expires="+ d.toUTCString();
   document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/";
 } 

$(document).ready(function () {

   // Hide the site search box
   $("#SearchBox").hide();

   var red, yellow;

   var siteurl = _spPageContextInfo.webAbsoluteUrl;

        // Get score colors
        $.ajax({
               url: siteurl + "/_api/web/lists/getbytitle('GMP Defaults')/items",
               method: "GET",
               headers: { "Accept": "application/json; odata=verbose" },
               success: function (data) {
                      // Process cookies
                   }, 
              error: function (data) {
                  alert("Error: "+ data);
             }
      });

});
</script>

This is being done in a subsite. The code is in the subsite's landing page in a Script Editor web part and cookies.js would be in Site Assets.

I've tried many different relative URLs and even put an absolute URL in the src statement and nothing seems to work.

Most of the examples doing something similar are using master pages and Style Library which have different relative URLs.

I thought I had a solution when I saw Mark Rackley's work using a content editor web part with the same subsite setup

   <script src="../../SiteAssets/cookies.js" type="text/javascript"></script>

but that doesn't work either. I want to do the same thing with other common code in View and Form pages but if I can't it get to work here...

  • I typically do it more explicitly and don't encounter problems, src="/managedpath/site/subsite/siteassets/cookies.js" – Eric Alexander Sep 2 '16 at 0:55
  • While probably not relevant this is O365 – maxwondercorn Sep 2 '16 at 1:18
  • i do it the same way, on prem, O365, no issues – Eric Alexander Sep 2 '16 at 2:35
  • Less documented, way more powerfull. If you can't add them to the MasterPage, add them as UserCustomAction ScriptLinks to the SiteCollection (script file will be loaded for all SC pages) or Web (scriptfile will be loaded for one subWeb only): Easy to add with the Chrome Extension: github.com/tavikukko/Chrome-SP-Editor NOTE: These do NOT get executed on New Experiences; Microsoft does not allow loading them (or any branding on NX) – Danny '365CSI' Engelman Sep 2 '16 at 7:38
2

Consider that

  • The Parent Site URL is : http://SPQassas/
  • The SubSite URL is : http://SPQassas/TeamSite

If your script file has been located at site asset library of the same subsite http://SPQassas/TeamSite , use the following

<script src="../SiteAssets/cookies.js" type="text/javascript"></script>

If your script file has been located at site asset library of the parent site http://SPQassas/ and you need to use it in your subsite http://SPQassas/TeamSite, use the following

<script src="../../SiteAssets/cookies.js" type="text/javascript">

If it still not working , so the issue is not related to the path it's related to the JS file itself, try to use another JS file with the above relative path guides and check if it's worked or not.

  • Not working even with other js files, so something I'm doing something fundamentally wrong. The path <script src="../SiteAssets/cookies.js" type="text/javascript"> is correct because I can link to a CSS file in a SiteAssests sub – maxwondercorn Sep 8 '16 at 17:36
  • let's investigate your issue > Where's you set this file cookies.js ? also should you upload this file cookies.js to internet to try it in my environemnt – Mohamed El-Qassas MVP Sep 8 '16 at 17:40
  • I got the folder is This folder is empty ?!!! please make sure it's uploaded !! – Mohamed El-Qassas MVP Sep 8 '16 at 18:18
  • try again please – maxwondercorn Sep 8 '16 at 20:23

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.