0

I have created an HTML snippet which references a js file stored in site assets folder.

The path to this file in my snippet is as

../SiteAssets/MyJsFile.js

In SharePoint, welcome page is set to [siteURL]/pages/home.aspx

Now In home.aspx, I have added a content editor web part having reference to above HTML snippet.

If I open home page using [siteURL]/pages/home.aspx everything works perfectly, but if I open site using [siteUrl] in the browser then it starts giving 404 error for my js file.

How can I resolve this issue, does it mean in SP we can not give a relative path for the home page?

4
  • Is siteassets library on the root of the site? Then just use /siteassets/myjsfile.js Commented Aug 14, 2016 at 11:46
  • aspx pages are in Pages library, while html and js files are in site assets folder.
    – nbi
    Commented Aug 14, 2016 at 11:47
  • I have this exact same problem. Did you come up with a decent solution, other than having to hard code the full path? I wish we could prevent SP from using the "/" URL for site home (Welcome) pages. i.e. always force the full path, including /pages/default.aspx... Commented Oct 12, 2016 at 21:08
  • nope. For home page I had to hard code the path, but for all other pages I am using relative path.
    – nbi
    Commented Oct 13, 2016 at 7:02

2 Answers 2

1

Always refer the JS/CSS paths either with SP tags or starts with site url

for eaxmple. Site URL: http://www.oursharepointsite/ JS Ref URl : "/SiteAssets/MyJsFile.js"

SubSite URL: http://www.oursharepointsite/subsite/ JS Ref URl : "/subsite/SiteAssets/MyJsFile.js"

It works wherever your page resides, no matter how you refer the file either with Designer or webpart.

4
  • could you please explain why ../ wont work?
    – nbi
    Commented Aug 14, 2016 at 13:08
  • 1
    Due to Alternate access mapping which redirects to welcome page based on zone
    – Shiva
    Commented Aug 14, 2016 at 13:11
  • Thanks Shive, but it makes me update my html when ever I have to deploy other site :(
    – nbi
    Commented Aug 14, 2016 at 13:17
  • in this case you can try sharepoint token... ~site/SiteAssets/MyJsFile.js, Try..;like we do in webpart JS link
    – Shiva
    Commented Aug 14, 2016 at 13:18
0

Consider you have a Root Site and your JS files within SiteAssets library in it http://rootsite

  • So the relative URL should be

/SiteAssets/MyJsFile.js

If your JS files within a SiteAssets library in a subsite http://rootsite/subsite and you need to use it at home page of root site,

  • So the relative URL should be
../SiteAssets/MyJsFile.js

Based on your question you should use /SiteAssets/MyJsFile.js instead of ../SiteAssets/MyJsFile.js

3
  • why does ../ wont work?
    – nbi
    Commented Aug 14, 2016 at 13:08
  • As I mentioned above it depends on the location of your JS files every ../ represent subsite , Is you tried to use /SiteAssets/MyJsFile.js is it worked ? please don't hesitate if you need more clarification Commented Aug 14, 2016 at 13:16
  • Yes it works, but the problem is if I need to use this in other SPO site, then I have to update the references in the HTML file
    – nbi
    Commented Aug 14, 2016 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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