1

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
reva
  • 626
  • 1
  • 7
  • 17
  • Can you explain what you mean by 'dynamic?' Is this per user? Per request? Where is the encryption done? – Dave Wise Apr 08 '11 at 20:49
  • Dynamic == Runtime, This is per user and the encryption is done for each request before generate the js – reva Apr 08 '11 at 21:04
  • With something that dynamic, you will want to also make sure that page caching is disabled. – Dave Wise Apr 08 '11 at 22:23

2 Answers2

8
  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" />
  </Module>
</Elements>

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Location="ScriptLink"
    ScriptSrc="~SiteCollection/Style Library/Boostrap/bootstrap.js"
    Sequence="100">
  </CustomAction>
</Elements>
Chris Beckett
  • 3,809
  • 17
  • 13
3

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.

Dave Wise
  • 13,181
  • 18
  • 19