How to update an iframe without refreshing the page?

0

I'm doing a system, more precisely, an HTML HTML and CSS editor, in which I need to send two fields via POST to a php page, which will process the data and save it to a random-named html file, generated at each page refresh.

So far so good, I'm sending this data via AJAX to prevent the page from refreshing and the file name changes.

On the same page there is a iframe referring to the html page created, except that when sending the data via AJAX and saving to the html file, I should give reload in iframe to update the file to which it references, like this:

document.getElementById("myIframe").contentWindow.location.reload();

The problem is there, at the time of giving reload in iframe , the whole page is updated (I say this because the loading icon appears as in the image below.)

What brings about the generation of a new filename, which, as I said, with each page refresh a new name is generated, and the src change of the iframe , pointing to a new file in white and not to the file that contained the code.

    
asked by anonymous 21.01.2015 / 13:12

1 answer

4

Well, nowadays I do not encourage the use of iframes , after I discovered some errors that it may entail regarding the passing of parameters and everything else ....

But a while ago I found the following solution:

function carregaiframe(src, id) {
   window.open(src, id);
}

The function call I do this:

carregaiframe("nomeSite.php?parametro=" + paramentro,"nomeDoIframe");

Whenever you call the function, it will load iframe again.

For this type of need, I recommend using Ajax, instead of loading the content into a iframe , you load it into a div

As follows:

$.ajax({
    type: "GET",
    data: { 
        parametros1: parametro1,
        parametros2: parametro2
        },
    url: "conteudo.php",
    success: function(data) {
        $('#conteudo').html(data);
    }
});

In the HTML page, just reference your JS add the JQuery lib and add the following line:

<div id="conteudo"></div>

Why is it better to work with AJAX than with Iframes ?

21.01.2015 / 13:18