Before when I used SharePoint 2013, opening HTML files would open up in the browser and my JavaScript code works. Recently, my organization switched to SharePoint Online and I noticed the HTML files in my Site Assets were opening up in a Previewer and none of the JavaScript code works. I've tried changing the extension to .aspx but that just force downloads the file. How can I get my HTML files to open in the browser and have my JavaScript code work in SharePoint Online?
2 Answers
Not Sure if you have been moved SharePoint online to Modern sites and Classic Sites. Below are the approaches to use Custom JS.
With Modern Sharepoint Site Pages:
Modern Sharepoint Pages does not support integrating Custom JS code directly. You must have to use modern script editor web parts Like react-script-editor OR Modern CEWP by SPJS and add your custom JavaScript code.
With Classic Sharepoint Site Pages:
when you are using classic Sharepoint pages on the Team site then you should enable custom scripts from Sharepoint Admin Center in your tenant and use Content editor OR Script editor web part.
I hope this solution should work for you.
I agree with Sajid Khan.
You can preview the custom JS files(ex: html file) that uploaded to the Document Library in SharePoint Online, but you cannot execute JavaScript code.
According to my research, SharePoint Online does not support integrating Custom JS code directly.
- On the Modern experience: You can use modern script editor web part, ex: react-script-editor
- On the Classic experience: You can use Content Editor web part or Script Editor web part.