Forum Discussion
Proper way to inject the JavaScript to the SPO page
Hi ganeshsanap,
I use the classic experience.
Unfortunately even though I've tried to run the script after page loaded, the issue still persist.
It looks like something is buffered in the browser and I have to make couple flips to make it work.
Have a look:
- HTML file
<h1><p id="container2"></p></h1>
<script type="text/javascript" src="https://...../JavaScript/InjectTest.js"></script>
- JS file
function runAfterEverythingElse() {
document.getElementById("container2").innerHTML = 'Test2';
}
_spBodyOnLoadFunctionNames.push("runAfterEverythingElse");
- On the page I have Test2 text as expected
- Now I change the text in the JS file, unfortunately it has no effect on the page after save
function runAfterEverythingElse() {
document.getElementById("container2").innerHTML = 'TEST AFTER CHANGES';
}
- Now I can open the JS file in the browser and I can see there are values before the change, but when I refresh the page, new values appear
Now I can refresh the page (Ctrl+F5) and new values will be on place
Shibato Every time you make changes to JavaScript files added in document library (Site Assets or any other library), you have to refresh the site page to load the latest version of JavaScript file.
Also, in case you have approval or check-out on files in library, make sure you publish the JavaScript files after making the changes.
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.
- ShibatoNov 22, 2022Copper Contributor
ganeshsanap wrote:Shibato Every time you make changes to JavaScript files added in document library (Site Assets or any other library), you have to refresh the site page to load the latest version of JavaScript file.
Is there any way to automatise that process or have I manually open the file and refresh it in the browser.
On the developing level, let's say I can live with that, but from the user perspective...
As I understand he won't get the new version on the JS file, until he won't refresh the file on his browser?Am I understand it correctly?
If yes, it looks putting the JavScript directly to the HTML file is the only reasonable way.
- ganeshsanapNov 22, 2022MVP
Shibato No, user don't need to refresh the JS file. User has to visit the site page only. Every time user opens the page, they will see the latest published version of JS file automatically.
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.