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

1 vote
Accepted answer
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.

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}

$.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 Feb 06, 2019 in Confluence

Try out the new editing experience

Hi team, I’m Avinoam, a product manager on Confluence Cloud, and today I’m really excited to let the Community know that all customers can now try out the new editing experience and see some of the ...

1,070 views 56 8
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