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

I need to add some javascript to the header section of some Tridion pages. I can create a separate set of MasterPage and template to achieve this, but it seems it is so much work and there are too many steps/templates just to achieve this. What is the simplest and fastest way to achieve this?

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

There's an infinite number of ways to achieve this. What is best depends entirely on your scenario; however, if you are looking simply to inject a snippet of Javascript into a manually-selected set of pages, the easiest and most flexible way I can think to achieve this would be along the lines of the following:

  1. Create a 'Plain Text' or 'Code' Schema, which contains a single, multi-line text field (this is where your Javascript goes).
  2. Add a Component Link field to your Page Metadata Schema called something along the lines of 'Header Javascript', supporting the above.
  3. Conditionally render out the contents of the linked Component in your Page Template Design.

This'll let you add JS on a per-page basis without having to create duplicate templates (and without having to duplicate the JS itself).

Of course, there are all kinds of variations you could implement dependent on what's most appropriate for your scenario (such as using Multimedia Components for your Javascript if you need the ability to hook up continuous integration, or - if the script is always the same - hardcoding the Javascript directly into the page template and using an 'Include header Javascript Yes/No' field in your Metadata to simplify the process and shield it from content editors).

share|improve this answer
add comment

The simplest way to add JS to your header in your Tridion implementation is to keep JS out of the Tridion CM and deal with it purely on the presentation side. To do this, simply render the path to the file on your presentation server. This means keeping your JS bundled along with other presentation side code.

See this Q&A for further insight on the subject: What is the best way to manage Javascript and CSS withTridion (2011/2013)?

share|improve this answer
    
This is a good point - not only because content managing script files is "cheating" but because it can add unnecessary complication where simply deploying to the presentation server will suffice. –  Ant P Dec 18 '13 at 12:40
add comment

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.