Dynamic script
tag injection is discussed comprehensively here.
Here the utility library loadScript project on GitHub is employed to keep the source code clean.
Again note that although a callback is invoked when the library is loaded by the browser the VisitorAPI is not available until the global callback onYalstVisitorAPILoaded
is invoked. To avoid globals the library should be loaded as a RequireJS module instead.
Self-hosting the utility library is recommended to reduce dependencies
<!-- self-hosting the utility library is recommended to reduce dependencies -->
<script src='loadScript.js'></script>
<script>
var YALST_INSTALLATION_URL = "//YALST_PRODUCT_SERVER/YALST_DIRECTORY";
loadScript(YALST_INSTALLATION_URL + "/visitor_api/v2.1/LiveSupport.min.js", function(error)
{
if (error)
{
console.log("Error loading VisitorAPI library:" + error.message);
}
}
);
function onYalstVisitorAPILoaded(api)
{
// The API is now globally available in LiveSupport.VisitorAPI
// as well as in the first function argument api.
}
</script>