Confluence REST API - how to make external links open in new tab?

Piotr Bajger February 11, 2019

Hi,

I'm creating Confluence pages using the REST API and everything works great, but how can I make external links on these pages to open in a new window/tab?

What I've tried in my XHTML is:

<a href="http://www.google.com" target="_blank">External link</a>

 but the "target" property gets stripped upon submission.

I was thinking about doing the same thing with jQuery (as described here):

<script>
jQuery(document).ready(function() {
    jQuery(".external-link").attr("target", "_blank");
});
</script> 

 but I'm unsure on how to inject this <script> into my page using the REST API.

 

I know this issue came up quite a few times here, but none of the answers addressed the REST API part of my question.

 

Thanks,
Piotr

1 answer

1 accepted

1 vote
Answer accepted
Bill Bailey
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.
February 11, 2019

Well I suggest cheating -  create a simple user macro that formats the link how you want. Then your REST call can just inject the user macro.

  <ac:structured-macro ac:name="link_macro" ac:schema-version="1">
    <ac:parameter ac:name="link">http://www.google.com</ac:parameter>
<ac:parameter ac:name="link-text">External Link</ac:parameter>
  </ac:structured-macro>

 You could also add any custom CSS or classes to your macro to have a different styling for these type of links.

Piotr Bajger February 12, 2019

Thanks, Bill, that's a great workaround.

Sadly the macros are not enabled for me, so I'd prefer a solution which doesn't use one -- do you think that's possible?

Thanks,
Piotr

Bill Bailey
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.
February 12, 2019

Well the problem you have is the one you are seeing -  Confluence stripping some constructs from your XHTML. I have tried editing the source view of a page, to add attributes (for example, resetting a list number) -  but as soon as the code is saved, what I have added has been filtered out.

And one way to inject the javascript on a page is via a user macro, or you need access to the admin console to add it across the site.

Piotr Bajger February 13, 2019

Thanks, Bill, I guess we'll have to live with ctrl+left click for now.

Piotr 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events