dimanche 17 mars 2019

use javascript to write a random

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.




Aucun commentaire:

Enregistrer un commentaire