Execute javascript code when page is loaded


I’m writing a plugin that adds some Information to the issue details view.

Therefore I’m using javascript code to add interactions.

my vm-template looks roughly like this (first HTML-Elements then the javascript parts)

<div id="myContent">my content</div>

document.getElementById("myContent").innerHTML = "my new content";

the point is that JIRA seems to reassemble the order of the elements. When I test my plugin the getElementById() always fails, because the corresponding element is not yet available in the DOM.

btw. using jQuery(document).ready(function() {...}); doesn’t make any difference. Using some blocking interaction in my javascript (e.g. alert()) shows that when it’s executed the JIRA-page is not yet complete. It is incomplete until the blocking event of my code returns (see attached screenshot)

So my question is: how can I ensure, that certain parts of the DOM are available when my script is executed.

1 Like

In your javascript, inject the ‘jira/util/events’ module and bind to NEW_CONTENT_ADDED with the reason ‘pageLoaded’. That’s how I do it.

 	 ], function(Events, EventTypes, EventReasons) {

	"use strict";
    Events.bind(EventTypes.NEW_CONTENT_ADDED, function(e, jiraContext, reason) {
        // Do your stuff here but check for the reason first to avoid being called too often


thank you for your quick response Yves.
I managed to get the code working and hook into the events.
But it is odd. I receive the events at the usual issue view page (e.g. http://localhost:2990/jira/browse/TEST-1)
However if I am looking at the issue details (not the list view) in a search result (e.g. http://localhost:2990/jira/browse/TEST-1?jql=resolution%20%3D%20unresolved) the events do not fire until I use the build-in refresh button at the bottom of the results list.
I am really confused about this behavour.

With JIRA 7.x, I receive the pageLoaded reason in the search view (details view) as well. You may also check for the “panelRefreshed” reason. Sometimes you need a combination of the two. One thing I realized over the years is that sometimes unexpected JIRA events are thrown and sometimes not :slight_smile: Listening to multiple events usually do the tricks.


Not sure if you solved the problem but I encountered a similar problem where I would not receive the NEW_CONTENT_ADDED event in the bulk edit view and only under Firefox. Turns out that the event is actually raised but before our add-on had the time to register its hook.

Jira sends the event from the observer.js file. This file doesn’t use require with dependencies. Instead, if get Events, EventType, etc via independent calls to require and send the event on document ready. The problem is that in Firefox (also happened once in Chrome), the event is sent before my module had the time to be initialized.

I tested that if I do the same thing that they did in observer, I’m able to receive the call.