There are at least three main ways to change the contents of a frame
or iframe
.
Change the src
attribute with JavaScript
Imagine you have a frame like this:
<iframe id="frame" src="http://servidor/pagina1"/>
Thenyoucanimplementafunctionthataccessestheframethroughitsid
andchangesthesrc
attribute,whichcontainsthedisplayedURL:
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>
Through a link ( a
) with the attribute target
Create a frame with the name
attribute set as follows:
<iframe name="frame" src="http://doc.jsfiddle.net/"/>
Thenyoucanopenapageonitwhentheuserclicksonalink,justsettheURLonthelinkandaddthetarget
attributewiththesamevalueasthe%frame%.
Example:
<a href="http://blog.jsfiddle.net/" target="frame">Trocar outro conteúdo</a>
Through a form submit with the name
attribute%
Considering the same previous frame with the attribute target
:
<iframe name="frame" src="http://doc.jsfiddle.net/"/>
Soyoucanopenapageonitwhentheusersubmitsaform,justsettheURLinthetributename
andtheaction
attributewiththesamevalueasthetarget
oftheframe.
Example:
<form action="http://jsfiddle.net/" target="frame">
<button type="submit" onclick="trocaPagina()">Trocar conteúdo</button>
</form>