Vary Script within Div

1

I would like to embed a script quiz in the sidebar of my site, it is 3 quiz in fact and I did not want to put one below the other, I would like them to switch between them automatically every time the page loads, occupying the same block.

It is a script with variation as below:

<script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/9527749.js"></script><noscript><ahref="http://polldaddy.com/poll/9527749/">O que faz você fugir da dieta?</a></noscript>

I would like a type script that I use to vary the images that varies every time the page loads. When clicking it takes you to the destination site, this option would not be necessary.

'<script>// <![CDATA[
var max = 3;
var nrImages = 3;
function makeImages() {
this[0] = "imagem1.jpg";
this[1] = "imagem2.jpg";
this[2] = "imagem3.jpg";
this.length = nrImages;
}
function makeLinks() {
this[0] = "Link pro site";
this[1] = "Link pro site";
this[2] = "Link pro site";
this.length = nrImages;

}
var vetImages = new makeImages();
var vetLinks = new makeLinks();
var x = Math.round(Math.random()*max);
var y = max / nrImages;
for(var cont = 1;cont*y<= max;cont++) {
if (x <= (cont*y)) {
document.write("<a href="+vetLinks[cont-1]+" rel=nofollow target=_blank><img src="+vetImages[cont-1]+" border='0'/></a>");
break;
}
}
// ]]></script>'
    
asked by anonymous 18.10.2016 / 19:53

1 answer

1

You can create the script tag dynamically and add to an element, a div for example:

<div id="scriptQuiz"></div>

Javascript:

var link = "<a href="http://polldaddy.com/poll/9527749/">O que faz você fugir da dieta?</a>";
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "http://static.polldaddy.com/p/9527749.js";
s.innerHTML = link;
document.getElementById("scriptQuiz").innerHTML = "";
document.getElementById("scriptQuiz").appendChild(s);

Of course, you can also generate href of link also dynamically, just adapt.

    
18.10.2016 / 20:11