Forum Moderators: open
Now Here’s a challenge…
I have a situation where I have 2 or 3 different js scripts and I need to call them randomly.
In other words a different js script such as
<script type="text/javascript" src="option1.js"> </script>
or
<script type="text/javascript" src="option2.js"> </script>
or
<script type="text/javascript" src="option3.js"> </script>
Will randomly load when ever user visits the page or refreshes the page.
This basically will work the same way as if you were to randlomly load images based on page refresh, except the script I am looking for will load js script instead of images.
Does anyone have any code examples on how I could accomplish this?
Thanks. Please Advise.
<html>
<head>
<title>
Random Script
</title>
<script type="text/javascript">
var srcs=new Array("option1.js","option3.js","option2.js","xyz.js")//Script sources
var randomScript
if(randomScript=document.createElement('script'))
{
randomScript.type="text/Javascript"
randomScript.src=srcs[(Math.floor(Math.random()*srcs.length))];
document.getElementsByTagName('head')[0].appendChild(randomScript);
}
</script>
</head>
<body>
hello
</body>
</html>