How to include some JavaScript for a jira custom field?

Sorin Sbarnea (Citrix)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2013

I tried to add some JavaScript inside a custom field description and it worked well inside the custom field editor.

Still, it seems that Jira stripped my javascript from the description when it was displayed in the issue UI (as apposed to the admin area, where it did not).

So, I would say this is a bug, now the question is how to workaround this and have my code executed.

3 answers

1 vote
Pierre Sauveur July 30, 2013

In complement to my previous intervention, I have just found how a workaround to my issue:

<script type="text/javascript">
(function($) {  
    AJS.toInit(function(){
        do stuff...
      });
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context) {
        do stuff again... 
    });
    // ------------------ Below the new line
    $(selector_of_the_element_to_modify).live("click", do stuff agin...)
})(AJS.$);
 </script>

I thought the .live would be sufficient, but the rest of the code provided by John Burns is still required: the call to .toInit and .bind handles the case when the create popup opens with the CF already shown, and the call to .live handles the case when the project/issue type needs to be changed to display the CF

Not sure how this can be used to do something else than adding handlers to specific elements, though

0 votes
John Burns
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2013

Try using this template:

<script type="text/javascript">
(function($) {   
    AJS.toInit(function(){
        do stuff...
      });
   
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context) {
        do stuff again...  
    });
})(AJS.$);
 </script>

Sorin Sbarnea (Citrix)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2013

I tried and the code does not reach the UI or Create/Edit issue. Still, anything that is outside the script tags does reach. Are you sure that you tested this with Jira 5.2?

John Burns
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2013

Yes, I am on 5.2, and it works in my instance. You might need to open a support ticket on this one.

Sorin Sbarnea (Citrix)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2013

I am retrying now on a test instance by enabling the safe mode, maybe there is somethiing that does this.

Pierre Sauveur July 29, 2013

John's answer worked for my case (Jira 5.0.7, trying to set onclick actions for a CF), thank you very much John !

Still there is a case when it is not enough: my CF is only available for one issue type (New feature). When the Create popup opens with another issue type selected and I change it to New Feature, the script seems not to run. Not a real problem for my case, I'll eventually try to find a workaround

Stefan Niedermann March 24, 2014

Hi,

did you find a workaround? I have currently the exactly same issue and for me it _is_ a problem ;-)

0 votes
Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2013

Have you included it in the correct field configuration? Editing description under custom fields is not enough.

Sorin Sbarnea (Citrix)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2013

I think it should be enough, as the description I entered does appear inside the Create/Edit screen, it is updated when I modify it but it does not contain the script part `<script>...</script>`. Which, by the way is included in the list of custom fields in the admin. I think this is related to the fact that in new jira (5.x), the Create/Edit screens are created from JSON instead of being rendered on the server side.

Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2013

Can you try adding it in the field configuration and see how it works? I can certainly find mine in the create/edit screens. Even on Create/Edit, rendering is done on server side.

Sorin Sbarnea (Citrix)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2013

I tried, no change the script section is removed but the rest is kept.

Suggest an answer

Log in or Sign up to answer