Come sostituire un IFrame in JavaScript

August 20

Un frame in linea, o iFrame, è un oggetto in una pagina Web che può contenere un altro oggetto, come ad esempio un'altra pagina Web. Con JavaScript, si ha la possibilità di sovrascrivere, o la modifica, il contenuto di ciò che viene visualizzato nella iFrame modificando l'attributo "src" del tag iFrame. È possibile consentire all'utente di sovrascrivere il iFrame associando tale funzionalità con un pulsante, per esempio.

istruzione

1 Creare un nuovo file HTML in un editor o utilizzando il Blocco note. Ad esempio, digitare:

<! DOCTYPE HTML PUBLIC "- // W3C // DTD XHTML 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text / html; charset = utf-8" />

<Title> Overriding iFrame </ title>

</ Head>

<Body>

</ Body>

</ Html>

2 Aggiungere un iframe per la pagina Web e assegnare un ID ad esso. Aggiungere un pulsante alla pagina Web di ignorare il iFrame. Ad esempio, tra il codice HTML "<body>" tag, digitare:

<Form> <input type = nome "pulsante" = valore "override" = "override" onclick = "changeIFrame ()" /> </ form>

<Hr />

<Iframe id = "Telegramma1" tag nel documento HTML. Ad esempio, digitare:

<Script type = "text / javascript">

Funzione changeIFrame () {

var cur = document.getElementById("frame1").src;

if (cur.match (/ yahoo /))

. Document.getElementById ( "Frame1") src = "http://google.com";

altro

document.getElementById ( "Frame1") .src = "http://yahoo.com";

}

</ Script>

Consigli e avvertenze

  • Assicurati di attendere fino a quando i carichi iFrame prima di tentare di accedere ai suoi elementi.