Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
  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>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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