How to add custom Javascript in Confluence pages?
Asked by Roland Benedetti:

I figured out how to add javascript calles in the header or footer but can't figure out how to add some Javascript in the pages itself.

Anybody knows?


Answers:
author: Matt Ryall
body: You could write a [User Macro](http://confluence.atlassian.com/display/DOC/Writing+User+Macros) to do it. You need to pick a name, set the output format as "HTML" and put your JavaScript content in the body. Then you just put this macro into your pages.

If you need different JavaScript on every page, Confluence ships with a disabled [HTML macro](http://confluence.atlassian.com/display/DOC/Enabling+HTML+macros) that you can enable in the "Plugins" list under the Administration. Then you can write an {html} macro with the \<script\> tags inside it.

comments: author: Roland Benedetti
body: Hi Thanks Matt,

But this means the Macro should be added to all the pages manually, I was more looking to add something on all pages, once for all.

Any idea?


author: Matt Ryall
body: In that case, I'm not sure what your question means. If you have added JavaScript in the header or footer via Administration \> Custom HTML, you should find it already on every page.


author: David at David Simpson Apps
body: This question is very similar or at least related to:

https://answers.atlassian.com/questions/3054/javascript-inclusion


author: Colin Goudie
body: Roland, take a look at the web-resources Module type. If you add a plugin with one of these in there then you can have your own JS files added to all pages. Have a specific look at the contexts, such as **atl.general** for example

<http://confluence.atlassian.com/display/CONFDEV/Web+Resource+Module#WebResourceModule-WebResourceContexts>


author: Anders
body: ??? -html

\<SCRIPT type="text/javascript"\>

var element = document.getElementById('main-content'); element.style.backgroundColor = "#33CCCC"; \</SCRIPT\>


author: Yagnesh Bhat
body: ```
<web-resource key="somekey" name="some description">   <resource type="download" name="yourfile.js"    location="/js/your file.js"/>   <context>page</context></web-resource>
```

In your atlassian-plugin.xml , include your javascript as a web resource for **page** context as above.


author: Bob Kennedy
body: I tried adding custom code in Custome HTML but the code does not appear in the pages themselves.


author: Sean King
body: If you want Javascript on an individual page use the {html} macro. Otherwise for all pages add your script in appropriate place in the the CustomHTML screen of the Admin Console.


author: Nikhil Naoghare
body: Hi Roland,

You can try this example <http://come2niks.com/?p=542> this really works.

Thanks,

Nikhil

comments: author: Matthew J. Horn
body: Nikhil, this link no longer works.


author: Jay Ji
body: i tried this but didn't work.


author: jhammons
body: Hi Roland,

Please refer to the following document:

* <http://confluence.atlassian.com/display/JIRA/Fields+Allowing+Custom+HTML+or+JavaScript>

Be well,

Jason Hammons

Atlassian - San Francisco

comments: author: Jennifer German
body: That is for JIRA. This question is about Confluence. Therefore that link is not useful here.


Tags: confluence-development

javascript