It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

[CONF] How can I run some JS when the pagetree has finished loading?

I am writing a user macro and part of the code is going to target the active page in the page tree (which uses the {pagetree} macro) and apply some simple styling to it.

The code is as follows:

#set ($activePage = "#childrenspan$content.getIdAsString()-0")
<script type="text/javascript">   
    var AP = '${activePage}';

As you can see it's a mixture of VTL and JS - the VTL sets a variable and passes it to the JS, which is basically the ID for the current page in the page tree.

However, it is running before the page tree has loaded. This means that it's effectively returning nothing. If you run the code in the developer console after the page has finished loading then it works.

So the question is, how can I run some JS after the {pagetree} macro has finished loading?

3 answers

Here I shall add a completely horrible hacky solution, in the hope that its sheer hackiness encourages somebody from Atlassian to suggest the correct way to do this!

Add a script at the end of your page HEAD:

var oldInit = AJS.toInit;

AJS.toInit = function(A) {
// Do some code of our own
var AP = '${activePage}';

Since this code runs before the pagetree plugin is invoked (in batch.js), we hijack the onInit function to add our own code to run at the end.

As I can't edit my post, that should say "but it didn't have an effect".

(can't seem to delete)

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Teamwork

Listening out loud: how Fabian Lopez works on distributed teams

The start of the COVID-19 pandemic served as a forcing function to re-evaluate the way teams worked together. In fact, many companies are re-learning how to collaborate with their teams. As some move...

58 views 2 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you