Capture the elements through the ID, and create an action on the button, checking which next index should be clicked, at each click, sum a position, if it is equal to the url total, it restarts to the index 0 (top):
<button id="next">Proximo</button>
<iframe id="rotator" src="http://www.1.com.br" width='100%' height='100%'></iframe>
<script>
window.onload = function() {
/* Para capturar o elemento por
getElementById, independente da posição do script,
é necessário que o documento window tenha sido lido */
var urls = [
'http://www.1.com.br',
'http://www.2.com.br',
'http://www.3.com.br'
];
var index = 1;
var click_button = document.getElementById('next');
var el = document.getElementById('rotator');
click_button.onclick = function() {
if ( index === urls.length ) {
index = 0;
}
el.src = urls[index];
index += 1;
}
};
</script>
If you want to implement using PHP directly in javascript, can do so, but I do not recommend, prefer to send the data via POST or GET, but then would be for another question:
<?php $urls = array('http://www.1.com.br',
'http://www.2.com.br',
'http://www.3.com.br'); ?>
<script>
window.onload = function() {
/* Para capturar o elemento por
getElementById, independente da posição do script,
é necessário que o documento window tenha sido lido */
var urls = <?php echo '["'.implode('","', $urls).'"];'; ?>
var index = 1;
var click_button = document.getElementById('next');
var el = document.getElementById('rotator');
click_button.onclick = function() {
if ( index === urls.length ) {
index = 0;
}
el.src = urls[index];
index += 1;
}
};
</script>
Resolution for new cases:
Case 1:
<script>
window.onload = function() {
/* Para capturar o elemento por
getElementById, independente da posição do script,
é necessário que o documento window tenha sido lido */
var urls = [
'http://www.1.com.br',
'http://www.2.com.br',
'http://www.3.com.br'
];
var index = 1;
var click_button = document.getElementById('next');
var el = document.getElementById('rotator');
click_button.onclick = function() {
if ( index === urls.length -1 ) {
this.disabled = true;
}
el.src = urls[index];
index += 1;
}
};
</script>
Case 2:
It wasn’t exactly clear what the problem was. Because it loads every iframe on the same page, if it’s updating, it has nothing to do with the code I published, but with the rest of the code that exists in your application, I believe.
The answer you marked as correct does not do what you put in "update", does not return to the first link.
– Renan Gomes
but it is not to return to the first link :) as this written:
ao clicar novamente volta para a primeira url, deveria PARAR DE TER AÇÃO ao clicar no botão Proximo quando finalizar as urls do array
– Gislef
And it doesn’t come back, if it’s coming back it’s because you used Ivan’s code (which checks the index and, if it equals the total of links, goes back to the first). :)
– Renan Gomes
No, both Van and Lucas made changes to the responses during the update. If you followed, I confused and Lucas' was correct, even before I put the Update in my question
– Gislef
But I learned a lot from Ivan, great explanations, but the final result without updating the page I could understand from Lucas. Again thank you all
– Gislef