Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

javascript is not included when issue is viewed by clicking the link in issue navigator

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 20, 2015

Hi all,

I built an add-on in which there are some custom fields and each custom field has javascript blocks in template vm file. It is working as a charm in issue main view.

However if I search issues and click on the link to issue in issue navigator, it redirects me to a URL similar http://localhost/jira/browse/BAR-21?jql= and javascript does not work. When I debugged from developer console I realised that script block is not included to response html.

It works after refreshing the page or just go to http://localhost/jira/browse/BAR-21 

Anyone has any idea on what to do?

Thanks in advance

Tuncay

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Volodymyr Krupach
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.
November 20, 2015

Hi Tuncay,

That's because when you click issue from the Issue navigator the page is not reloaded and issue details are loaded/shown through ajax and JavaScript.

So you need to make sure your scripts are loaded to the Issue Navigator screen. You can achieve this by adding appropriate context to the web-resource that declares your JavaScript:

<web-resource>
  ...
  <context>jira.navigator.advanced</context>
  <context>jira.navigator.simple</context>
</web-resource>

 

============ Update ================

To catch issue switch event in Issue Navigator and fire a code when issue is switched:

1) declare web-resource that loads a script into navigator context:

<web-resource key="cust-field-resource" name="cust-field-resource">
  <dependency>com.atlassian.auiplugin:ajs</dependency>
  <resource type="download" name="issue-nav.js" location="/js/issue-nav.js" />
  <context>jira.navigator.advanced</context>
  <context>jira.navigator.simple</context>
</web-resource

2) In the script file add a code to listen for issue change:

AJS.$(function() {
  JIRA.Issues.Application.on("issueEditor:loadComplete", function(model, props) {
    if (!this.standalone && !props.loadReason) {
      alert("Do stuff");
    }
  })
});

 

This code works for JIRA 6.3.x and 6.4.x and does NOT work under JIRA 7.0.0. I did not test it under other JIRA versions. Please contribute if you find how to do the same under 7.0.0.

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 20, 2015

Hi Volodymyr, Thanks for your reply. I am aware that the page is loaded by AJAX, and already tried to add those contexts you mentioned. However this is custom field and I do not know how to add those contexts to custom field in atlassian-plugin.xml Any idea on this?

Volodymyr Krupach
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.
November 20, 2015

Can you separate your JavaScript into web-resource? Posting your atlassian-plugin.xml may help me to understand the problem.

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 20, 2015

I appreciate your help. Assume I have custom field definition as below in atlassian-plugin.xml <customfield-type key="myFieldKey" name="My Custom Field" class="com.mycompany.MyCFType"> <resource type="velocity" name="view" location="templates/view.vm" /> <resource type="velocity" name="column-view" location="templates/list.vm" /> </customfield-type> In view.vm I am using javascript. What I understand from your comment is to - define <web-resource> - convert javascript block in vml file to a js file and add it to <web-resource> - in vml file load the web-resource

Volodymyr Krupach
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.
November 20, 2015

Do you mean that you have script block inside of your view.vm like: <script> // some script here </script> and it's not returned with html when you click the link from Issue View?

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 20, 2015

Yes, but not from issue view, from issue navigator

Volodymyr Krupach
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.
November 20, 2015

That's strange. Please post snippet of the view.vm.

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 20, 2015

#* @vtlvariable name="remoteUser" type="com.atlassian.crowd.embedded.api.User" *# #* @vtlvariable name="customField" type="com.atlassian.jira.issue.fields.CustomField" *# #* @vtlvariable name="labelUtil" type="com.atlassian.jira.issue.label.LabelUtil" *# <script type="text/javascript"> //script comment (function($) { some code AJS.$(document).ready(function () { .... }); </script> <some html elements here> I double checked, when I click on issue, and inspect source I can not find my script block. Thanks again

Volodymyr Krupach
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.
November 20, 2015

Did you try to move it below? If it does not help it's worth to try loading the web-resource in vm file.

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 20, 2015

Hi Volodymyr, Thanks for your help, and sorry I could not notice your answer. Here is what I did in the meantime I put all script block to a new js file. Assume I have my-script.js file. I added below lines to atlassian-plugin.xml <web-resource key="my-script-resource" name="My Plugin Web Resources"> <dependency>com.atlassian.auiplugin:ajs</dependency> <resource type="download" name="my-script.js" location="/assets/js/my-script.js"/> <context>jira.navigator.advanced</context> <context>jira.navigator.simple</context> </web-resource> Last, my vm file now looks like this #* @vtlvariable name="remoteUser" type="com.atlassian.crowd.embedded.api.User" *# #* @vtlvariable name="customField" type="com.atlassian.jira.issue.fields.CustomField" *# #* @vtlvariable name="labelUtil" type="com.atlassian.jira.issue.label.LabelUtil" *# #if ($value) <some-html-here /> #end When I open issue navigator (both detailed and list view) javascript does not run. If I refresh the page it is OK. I also tried adding $webResourceManager.requireResource("com.myplugin:my-script-resource") to the vm file, but no success.

Mateusz Szafarz November 21, 2015

It might sound silly but you can try add some invisible 'img' in your template file with attribute 'onload' containing your script. It works for me but I'm aware that it shouldn't be the way to do that. I use something like that: <img src="${baseUrl}/images/icons/bullet_notdone.gif" onload=" // your script " style="display: none;" width="1" height="1">

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 21, 2015

Thanks Mateusz for the brilliant idea. However I hate writing scripts between quotes/double quotes. You always have to care about quotes because you're already in quotes. But this technique is good for small scripts.

Volodymyr Krupach
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.
November 21, 2015

Hi Tuncay, I did a test for your case by adding "Before<script>alert(1)</script>After" to view.vm of my custom field. The alert box is shown in all cases: for Issue view screen and when changing issues though Issue Navigator. Although only "Before After" is shown in source for Issue Navigator case and that makes sense: When switching through Issue navigator the content of the issue is retrieved through Ajax and attached to the page and in this case inline JavaScript is not executed. I guess that the inline JavaScript is retrieved separately and attached to the page context. Moreover when switching through Issue Navigator I get 3 alerts and I did not find how to make to be executable only once. Adding $webResourceManager.requireResource("com.myplugin:my-script-resource") does not work for the Issue Navigator as well as defining the <context>.

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 21, 2015

Hi again Volodymyr and I really appreciate your help. You're right, script is being loaded but not seen in HTML content, anyway it is loaded. But I really have strange case, when I click on any issue on issue navigator, script runs twice (not 3), and what I expect from the script does not work, then I click on other issue again script runs twice. The interesting part is, when I click back to first issue script runs three times and result is OK. All issues that I click at least twice work OK. I prepared a screencast. In the screencast, I am expecting to see an inline dialog after clicking the inventory field (what the script does, I can share if you want). Here is the video, please count the alerts each time I click on issue and see if inline dialog appears. http://www.screencast.com/t/gRVAIqaVp

Volodymyr Krupach
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.
November 22, 2015

@Tuncay Senturk [Snapbytes], I updated the answer.

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 22, 2015

Hi Volodymyr, Really really appreciated. As you mentioned, it does not work with JIRA 7, but works in JIRA 6.x I will work on it and keep here updated if I find anything. Thanks so much again.

Volodymyr Krupach
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.
November 22, 2015

Hi Tuncay! Hint for JIRA 7.0.0: when you are on Issue Navigator page do global script search for "selectIssue". Looks like you need to define backbone controller and listen for this event. I do not have backbone experience so have not proceeded with it.

0 votes
Benito Jiménez September 3, 2018

Hi everyone, I'm having the same problem in JIRA 7.X. Did anyone manage to solve this problem? Thank you very much! Greetings.

TAGS
AUG Leaders

Atlassian Community Events