How do I use a .js attachment on a page?

I've been writing custom javascript code in {html}<script></script>{html} tags on the page or another and including it, I've made User Macros and all that has worked fine but I don't want to continue that route. Instead I want to upload my .js files as attachments and include them, the best way I see is

{html}&lt;script src="/download/attachments/123/file.js" type="text/javascript"&gt;&lt;/script&gt;{html}

That seemed too easy and apparently was, Chrome is throwing an error when trying to load it:

Refused to execute script from 'http://confluence/download/attachments/4426187/HotFixCreation.js' because its MIME type ('application/x-upload-data') is not executable, and strict MIME type checking is enabled.

Any ideas how I can use my .js attachments?

3 answers

1 accepted

This widget could not be displayed.
Davin Studer Community Champion Aug 15, 2014

Have you tried editing the mime type in the attachment properties?

I never clicked on properties before, it worked like a charm.

I never clicked on the properties before, worked like a charm.

This widget could not be displayed.

Not sure why it's not blocked but I can load it from JavaScript:

{html} &lt;script type="text/javascript"&gt; $.getScript('/download/attachments/123/file.js'); &lt;/script&gt; {html}

This widget could not be displayed.

$.getScript is to include the file content in the body of the page. It's to work around the cache... it solve the shituation, but it's not a best practice.

Yes, Davin,  edit the content type of the js file to "application/javascript" is a best solution!

To change the content type of an attachment, please visit the view page attachment (viewpageattachments.action) and click in Properties of the js file.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Monday in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

361 views 2 5
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you