Can’t you just escape the double quotes that you don’t want to terminate the string? \"
evaluates to the literal character "
.
document.write("<script src=\"" + (window.API_URL || "http://example.com/" + Math.random()) + "\"><\/script>');
3
solved Scripting a .write script containing HTML tags using only double quotes [duplicate]