Why do Javascript tags in Velocity templates sometimes get filtered?

I render a web-panel on the Jira issue view (location: atl.jira.view.issue.right.context). The velocity template looks as roughly follows:

<!--  some HTML here -->
<script type="text/javascript">
    // some javascript here
</script>

On the normal issue view (/browse/ISSUE-KEY) this works just fine, the HTML loads and the JS tag gets executed.

However, if the same panel gets loaded on the browse Jira project view, the HTML loads as expected but the JS tag seems to get magically removed from the velocity template.

Does anyone have an explanation for this? This inconsistency is driving me mad. I tried a bunch of variants to make this work with no success. E.g. to put the JS code in a separate JS file, create a web-resource for it in atlassian-plugin.xml, and load it via $webResourceManager.requireResourcesForContext("context-key")

1 Like

Hmm, no one else ever experienced this?

We’re facing this issue too. Tried web-resource too but none of them is working

I do have a very hacky workaround, just copy everything from the min.js you need to the onload method for the iframe.

1 Like

@chisiang.ng I kind of solved this but also with a workaround. Here is what I did:

  1. Extract the iframeResize() call to a separate JS file that looks something like this (leaving the script tag in the velocity template - because in the browse issue view it works):
/**
 * Resizes the iframe every time the user selects another issue in
 * the issue navigator.
 */
window.JIRA.bind(window.JIRA.Events.ISSUE_REFRESHED, function() {
    window.iFrameResize({heightCalculationMethod: "taggedElement"});
});
  1. Update the plugin descriptor with these 2 webresource definitions:
    <web-resource key="iframe-resizer-resource" name="iframe-resizer-resource">
        <resource type="download" name="iframeResizer.min.js" location="lib/iframeResizer.min.js"/>
        <context>jira.browse.project</context>
        <context>jira.view.issue</context>
    </web-resource>

    <web-resource key="resize-resource" name="resize-resource">
        <dependency>com.atlassian.auiplugin:ajs</dependency>
        <resource type="download" name="iframeResize.js" location="static/iframeResize.js">
            <property key="content-type" value="text/javascript"/>
        </resource>
        <context>atl.general</context>
        <context>jira.navigator.simple</context>
        <context>jira.navigator.advanced</context>
    </web-resource>

The context property will make sure these two files are included and executed on the required all pages. I left the script tag in the velocity template because for the view issue page that works. For the issue navigator iFrameResize() gets called from the included web resource.

It’s also not a very clean solution and I still don’t understand why the script tag gets removed in some views. It might be because in the issue navigator issues are loaded via Ajax…

1 Like

Thanks for sharing, i’ll try it out :grinning:

1 Like