Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do i get the amount of labels of current page with JavaScript?

Florian Wößner July 15, 2020

Hello,


I am looking for a way to get the number of labels from the current page that I have open in the editor.

This script runs when the editor is loaded:

// load editor first
AJS.bind("init.rte", function(){
var buttonID = "rte-button-publish";
//check if publish button exist
if(AJS.$("#" + buttonID).length > 0) {
//onClick
AJS.$("#" + buttonID).click(function (e) {
console.log("Clicked 'Publish' page button", this);
var labelbutton = "rte-button-labels";
// HOW DO I GET THE LABEL COUNT
if(AJS.$("#" + labelbutton).find(".trigger-text").text() == "Labels")
{
//show alert
alert("Please add at least one or more labels!");
console.log("Show Alert", this);
//Dont finish Action
e.preventDefault();
console.log("Default prevented", this);
e.stopPropagation();
console.log("Stoped Propagation", this);
}

});
}
});  

Basically only the last the if statement in the code above doesn't work for me.

I need something like:

if (LABELCOUNT == 0) {DO THIS;}

 

I am thankful for every help!

1 answer

1 accepted

2 votes
Answer accepted
Aidan Derossett [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.
August 6, 2020

Hey friend! :D

If you're trying to get the labels for a page, it may be easier for you to just make an AJAX call to the confluence REST API. Specifically, there's an API for labels, that will give you the label data for whatever page you specify. 

Here's a quick and dirty example of how this could work:

let pageId = (new URL(document.location)).searchParams.get("pageId")

AJS.$.ajax({
url: AJS.contextPath() + "/rest/api/content/" + pageId + "/label",
success: function(result){
console.log(result.size); //result.size is the number of labels
}});

Obviously you'd want to add all of your logic around that and clean it up, but I believe that will get you what you need.

Let me know if you have any questions or if you'd like me to clarify anything further!

Best,
Aidan

Florian Wößner August 24, 2020

Hey Aidan,

that works well if you want to edit an existing page.
Is there a way to get the number of labels from an unpublished site?
The problem is, when I create a new page, the page in the editor doesn't have a PageID yet.
That means, getting the amount with the REST API does not work

Florian Wößner August 24, 2020

Ok nevermind, I can use DraftId instead of pageID :D
Nevertheless, thank you very much for your help!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events