Vary Script within Div

Asked

Viewed 93 times

1

I would like to incorporate a quiz in script in the sidebar of my site, are 3 quiz actually and did not want to put one below the other, I would like them to automatically switch each other every time the page is loaded, 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><a href="http://polldaddy.com/poll/9527749/">O que faz você fugir da dieta?</a></noscript>

I would like a script type what I use to vary the images that varies every time the page is loaded. By clicking it leads to the destination site, this option would not be required.

'<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>'

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);

Sure, you can generate the href of link also dynamically, only adapt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.