views:

663

answers:

3

How do you load a URL from a text box into iframe in a HTML file via javascript?

+7  A: 
var oIFrame = document.getElementById("id_of_iframe");
oIFrame.src = document.getElementById("id_of_textbox").value;
marduk
I would modify that to add error checking at each step: `if (oIFrame != null)...`
Cerebrus
A: 

Or with document.frames if its the only iframe you are using:

var myIframe = window.document.frames[0]; // lets grab the iframe object
if (myIframe != null){
  myIframe.src = document.getElementById("id_of_textbox").value; // set the value as source.
}
vsync
A: 

Just to add I guess, via jQuery

$('iframe#guid').src( $('#textbox_id').val() );

with error checking:

var iframe = $('#guid');
if(iframe.length > 0){
    iframe.attr('src', $('#textbox_id').val());
}
Dmitri Farkov