0

I have this code below that pulls up FX data from the Visa website. Each time you tap on a date, it adds another row.

But adding a row seems to cause a reload/refresh of all the iframes ... why? How do I prevent that?

(You can see it live at http://zinclabs.com/eur2)

<body>
<script>

var date = new Date();

function format(date) {
    var mm = date.getUTCMonth() + 1;
    var dd = date.getUTCDate();
    var yy = date.getUTCFullYear();
    return mm + '/' + dd + '/' + yy;
}

function addrow(){
    var d = format(date);
    document.body.innerHTML +='<div class="row"><div class="date" onclick="addrow()">'+d+'</div><div class="rate"><iframe scrolling="no" src="http://usa.visa.com/support/consumer/travel-support/exchange-rate-calculator.html/?fromCurr=USD&toCurr=EUR&fee=0&exchangedate=' + d + '"></iframe></div></div>';
    date.setDate(date.getDate() - 1);
}

addrow();

</script>
</body>
Dan
  • 1,257
  • 2
  • 15
  • 31

1 Answers1

0

Adding text to an object's innerHTML causes a complete refresh of the object – in this case document.body. Therefore, all the iframes within it will be reloaded.

Instead, you could create a new DIV element, and append it as a child of document.body:

function addrow(){
  var d = format(date);
  var newRow= document.createElement('div');
  newRow.innerHTML= '<div class="row"><div class="date" onclick="addrow()">'+d+'</div><div class="rate"><iframe scrolling="no" src="http://usa.visa.com/support/consumer/travel-support/exchange-rate-calculator.html/?fromCurr=USD&toCurr=EUR&fee=0&exchangedate=' + d + '"></iframe></div></div>';
  document.body.appendChild(newRow);
  date.setDate(date.getDate() - 1);
}
Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79