There are at least three main ways to change the content of a frame
or iframe
.
Change the attribute src
javascript
Imagine you have a frame like this:
<iframe id="frame" src="http://servidor/pagina1" />
Then you can implement a function that accesses the frame through your id
and changes the attribute src
, containing the displayed URL:
function trocaPagina() {
var f = document.getElementById('frame');
f.src = 'http://servidor/pagina2';
}
This can be called through some link or button, for example:
<button type="button" onclick="trocaPagina()">Trocar conteúdo</button>
Via a link (a
) with the attribute target
Create a frame with the attribute name
defined as follows:
<iframe name="frame" src="http://doc.jsfiddle.net/" />
Then you can open on it any page when the user clicks on a link, just set the URL on the link and add the attribute target
of the same value as name
frame.
Example:
<a href="http://blog.jsfiddle.net/" target="frame">Trocar outro conteúdo</a>
Through a form Submit with the attribute target
Considering the same previous frame with the attribute name
:
<iframe name="frame" src="http://doc.jsfiddle.net/" />
Then you can open on it any page when the user submits a form, just set the URL action
and the attribute target
of the same value as name
frame.
Example:
<form action="http://jsfiddle.net/" target="frame">
<button type="submit" onclick="trocaPagina()">Trocar conteúdo</button>
</form>