How do I add a random 1 of 4 pre-defined <script>s to a webpage?
i.e. A 3rd party tool has 4 e-book download modal forms that are generated by a unique <script> (1 for each e-book).
I want to randomly add 1 of these <script>s to the page, so that on each page load, a different e-book download modal form is displayed.
How would I do this?
I've tried:
var min=1;
var max=4;
var random = Math.random() * (+max - +min) + +min;
switch ($random) {
case 1:
document.write(<script src=""></script>)
break;
but the </script> in the document.write closes the main <script>. Do I need to use HTML ascii code for the < & >?
Help appreciated.
Update:
The scripts I need added are in the format:
<script type='text/javascript' async='true' src='https://app.ontraport.com/js/ontraport/opt_assets/drivers/opf.js' data-opf-uid='p2c187780f17' data-opf-params='borderColor=#ec0c8d&borderSize=5px&formHeight=572&formWidth=60%&maxTriggers=2&onExitIntent=true&popPosition=mc&timeframe=1&instance=1067093054'></script>
'<\/script>'to avoid closing the script tag. But a better solution wouldn't usedocument.writeat all.