We have to inject dynamic javascript (contains a encrypted var value) into all web pages.

What is the best way to inject Dynamic Javascript using Master Page?

Other Details:

  • SharePoint 2010 STD
  • Master Page changes are allowed
  • Internal Application
  • Injected var value will be used to make ajax calls
  • Can you explain what you mean by 'dynamic?' Is this per user? Per request? Where is the encryption done?
    – Dave Wise
    Apr 8, 2011 at 20:49
  • Dynamic == Runtime, This is per user and the encryption is done for each request before generate the js
    – reva
    Apr 8, 2011 at 21:04
  • With something that dynamic, you will want to also make sure that page caching is disabled.
    – Dave Wise
    Apr 8, 2011 at 22:23

2 Answers 2


A simple approach would be to create a user control (code only, no ASCX required) that handles building and encrypting the text and then have it write out the dynamically generated javascript. From there, simply include that control somewhere in the body of your master page (not on a content placeholder).

Naturally, all of this should be done by creating and deploying a feature.

  1. Create a full-trust or sandbox solution in Visual Studio.
  2. Create a module element in Visual Studio to deploy a bootstap JavaScript file
  3. Create a custom action element to inject the link to the bootstrap JavaScript file
  4. Create a Feature that includes the two elements
  5. In your bootstrap Javascript file, inject any additional dynamic links (see below)
  6. Deploy your solutons and activate your feature

Now that you have your bootstrap JavaScript file injected, you can just edit the JavaScript file to modify which additional scripts and CSS are dynamically loaded.

There are a lot of blogs on how to inject dynamic JavaScript and CSS links into a page. here is a hyperlink to one: How To Dynamically Insert JavaScript and CSS

Here are examples of elements

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="Boostrap.Module" Url="Style Library" RootWebOnly="True">
    <File Path="Bootstrap.Module\bootstrap.js" Url="Bootstrap/bootstrap.js" />

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    ScriptSrc="~SiteCollection/Style Library/Boostrap/bootstrap.js"

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.