JSOM is not working in SharePoint Server 2019 / 2016
Actually, I have many issues related to JSOM code in SharePoint Server, and the main issue was mainly related to the order of SharePoint JS files, so in case, you need to use JS code in your SharePoint Site to access SharePoint Objects using SPContext, you have to make sure that the SharePoint JS files were listed in the below order
<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.init.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.core.js"></script>
Then try to call your function using SP.SOD.executeFunc('sp.js', 'SP.ClientContext', yourfunctionname);
or _spBodyOnLoadFunctionNames.push("yourfunctionname");
Example: Run JSOM script in SharePoint
<a href="#" onclick="RunJSOM();">Run JSOM in SharePoint</a>
So to get this JSOM code working in SharePoint, you have to do the following:
function RunJSOM(){
SP.SOD.executeFunc("sp.js", 'SP.ClientContext', myFunction);
}
function myFunction(){
//add your code
}
For more details, Please check JSOM code is not Working in SharePoint 2016/2019