2

I have site which is non root site i.e. url of site is: http:///sites/xxx

I want to place a content editor web part and refer a link to javascript file which is stored in the Style Library.

My code in Content Editor Web Part :

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

If I pass this it refers to file http://<Web_Application>/sites/Style%20Library/js/Scripts.js

When I place : <script language="javascript" src="/Style%20Library/js/Scripts.js" type="text/javascript"></script>

it refers to file http://<Web_Application>/Style%20Library/js/Scripts.js

What should I do to give the correct path to the JavaScript file?

  • other than making it a full URL, does it it anything else unexpected? – Ashish Patel May 9 '14 at 11:30
2

If can use _spPageContextInfo to grab the current web/site url. Prepend that url to your path to your JS.

For ex.

SP.SOD.executeOrDelayUntilScriptLoaded(LoadMyScript, 'sp.js');

function LoadMyScript() {
   var currWebUrl = _spPageContextInfo.webServerRelativeUrl; // use siteServerRelativeUrl for site-collection 
   //Dynamic loading of scripts
   var jq = document.createElement('script');
   jq.src = currWebUrl + "/Style%20Library/js/Scripts.js";
   document.getElementsByTagName('head')[0].appendChild(jq);
}
1

Check your code in content editor webpart, I think it should be /sites/xxx/style library....

If that doesn't work, try using the URL tokens ~site or ~sitecollection

Otherwise give full URL of your JS file.

You can also use spservices library from mark. It has some utility methods to get current site address. That's a long way though...

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.