This post is about injecting site relative JavaScript include files in SharePoint using CEWP. It is not about injecting JavaScript files using the server-side ASP.NET script manager or any other server-side mechanism.
We all know that the src attribute of the <script> element is either page relative or server relative; or God forbid, absolute. You cannot use the tilde (~) to make the URL site relative, but you can achieve this using the SharePoint page L_Menu_BaseUrl variable and inject the include file into the page dynamically:
<script type="text/javascript">
document.write('<script',
' src="' + L_Menu_BaseUrl +'/ScriptLibrary/SiteSpecificScript.js"',
' type="text/javascript"><\/script>');
</script>
This actually makes the include file URL server relative, but without hardcoding the site path into the URL - making it equivalent to being site relative.
Tuesday, June 16, 2009
Subscribe to:
Post Comments (Atom)

3 comments:
This should work:
src="/ScriptLibrary/jquery-1.3.2.min.js"
Christophe
Or maybe I misunderstood, and you were talking about linking to a script within the site. In this case I wouldn't use jQuery as an example, as this is typically a script that you want to put in a central location and share across sites...
Yes, you're right, I'll change the example to a site specific script.
This posting is just a sidebar to another post, where I recommend storing the jQuery library in a central place such as the top site of the root site-collection.
Post a Comment