JavaScript Forum

Changing iframe src, what's the best way

Apr 27, 2010 9:29 am
Joe Nine

I want to change the src of an iframe using JavaScript and would like to
know what's the most reliable way.

There's either directly accessing the iframe's .src property or there's
using the setAttribute method. I've read good and bad things about both.

I'll only need to be doing this on the 21st century browsers (IE6+,
Firefox 2+, Safari 3+, Opera 9+ and Chrome). So issues with old browsers
like NS3, IE4 etc won't be an issue.

Apr 27, 2010 10:02 am
Sean Kinsey
Re: changing iframe src, what's the best way

On Apr 27, 11:29?am, Joe Nine <j...@yahoo.com> wrote:
> I want to change the src of an iframe using JavaScript and would like to
> know what's the most reliable way.
>
> There's either directly accessing the iframe's .src property or there's
> using the setAttribute method. I've read good and bad things about both.
>
> I'll only need to be doing this on the 21st century browsers (IE6+,
> Firefox 2+, Safari 3+, Opera 9+ and Chrome). So issues with old browsers
> like NS3, IE4 etc won't be an issue.

Neither.
The src property is only used to set the initial url of the Iframe.
To set a new URL you use the iframes contentWindow
'iframeObj.contentWindow.location = foo;'


Apr 27, 2010 10:47 am
Joe Nine
Re: changing iframe src, what's the best way

Sean Kinsey wrote:
> On Apr 27, 11:29 am, Joe Nine <j...@yahoo.com> wrote:
>> I want to change the src of an iframe using JavaScript and would like to
>> know what's the most reliable way.
>>
>> There's either directly accessing the iframe's .src property or there's
>> using the setAttribute method. I've read good and bad things about both.
>>
>> I'll only need to be doing this on the 21st century browsers (IE6+,
>> Firefox 2+, Safari 3+, Opera 9+ and Chrome). So issues with old browsers
>> like NS3, IE4 etc won't be an issue.
>
> Neither.
> The src property is only used to set the initial url of the Iframe.
> To set a new URL you use the iframes contentWindow
> 'iframeObj.contentWindow.location = foo;'

I should have specified that the iframe was created with no src property
in the iframe tag, so it's debatable whether what I'd be doing would
meet your classification of "initial url".


Apr 27, 2010 10:51 am
Richard Cornford
Re: changing iframe src, what's the best way

On Apr 27, 11:02 am, Sean Kinsey wrote:
> On Apr 27, 11:29 am, Joe Nine wrote:
>
>> I want to change the src of an iframe using JavaScript and
>> would like to know what's the most reliable way.
>
>> There's either directly accessing the iframe's .src property
>> or there's using the setAttribute method. I've read good and
>> bad things about both.
>
>> I'll only need to be doing this on the 21st century browsers
>> (IE6+, Firefox 2+, Safari 3+, Opera 9+ and Chrome).

All Safari browsers, all Firefox browsers (and for that matter all
Gecko derived browsers) and opera versions from (at least) 6 on, are
"21st century browsers".

>> So issues
>> with old browsers like NS3, IE4 etc won't be an issue.
>
> Neither.
> The src property is only used to set the initial url of the Iframe.

Taking the question literally that is exactly what the OP asks to do.
The questionable sense in attempting that justifies dismissing it.
However, as the - src - property is specified in the W3C HTML DOM
standard and is not read only, if you wanted to set it then writing a
new value to the property of the IFRAME element object should be
acceptable (even if there are no technical reasons for expecting that
action to have useful consequences).

> To set a new URL you use the iframes contentWindow
> 'iframeObj.contentWindow.location = foo;'

In the W3C HTML level 2 DOM standard the HTMLIFrameElement interface
does not have a - contentWidnow - property, only- contentDocument -.
Though I think all of the listed sub-set of "21st century browsers" do
have that property.

However, assigning to the - location - property of a window/frame
object is OK for windows/frames containing HTML documents (and some
other sorts) but I don't think you will find the property in, say,
window/frame objects that contain PDF document. Obviously that is not
an issue if the intention is never to attempt to navigate the frame to
any non-HTML documents.

The most general, cross-browser, method of navigating any frame
(including an IFRAME) with scripts is to arrange that the frame has
both ID and NAME attributes that hold the same value and then access
its window/frame object through the - frames - collection of the
containing window/frame object, using that NAME/ID i.e.:-

frames['exampleFrameName'].location = 'new URL';

or (if you like):-

window.frames['exampleFrameName'].location = 'new URL';

And that formulation has been effective on every browser that has
supported both javascript and frames/iframes (including those that do
not have - contentWindow - properties and those were assigning the to
- src - property of an IFRAME element did not result in navigation).
It is one of the very few 'one code fits all' browser scripting
options.

(Note, this is not using - document.frames - as that was never
universally supported in frame capable browsers, and that the use of
the - location - property imposes the same caveats for (at least some)
non-HTML documents as your suggestion.)

Richard.




Previous Thread: Control horizontal scrolling
Next Thread: Clearing a global array

Related Forum Topics
Changing the style of a
I have an html page with some javascript that outputs text to a <td>
element. This works. Here is the element:

<td rowspan=2 valign=top id="out" class=op bgColor="white"></td>

I want to let the user change the appearance of this text, so I have
provided some buttons:

<tr><td>
...
Getting Value Without Changing HTML
I am looking at the "page source" for a page with non-starndard
looking HTML. The page source shows this:

<div id="field-emailhidden">
<INPUT TYPE="HIDDEN" NAME="Email$95Address" AnnType="Text Entry"
AnnRequired="false" AnnValidateEmail="false" VALUE="klitwak@apu.edu"
SIZE=32...
Opera 11 and changing input value
I have some js that looks like

window.onload = function() {
document.getElementsByTagName("input")[0].value = "me";
};

and the relevant HTML is simply

<input type="submit" name="submit" value="">

There is only *one* INPUT in the page. Alas Opera 11 only shows the
button with no...
JQuery changing document with?
Hi,

I have a website with a jQuery drop down on hover. Now when I load the page it seems to be all well aligned until i hover over the drop down tab from where the jQuery hover appears, from then on I get a horizontal scroll bar that doesn't disappear anymore as the drop down hides when hover...
Changing letters in boxes
Dear group members:

I wanted to accomplish something, and know javascript or jquery can do
the work, but my knowledge on both languages is too limited. If
possible, please help. My idea is in the following web page.

http://www.pinyinology.com/js_learning/letters.html

Thanks for your...
Changing styles values by Element Name
Is it possible to change a batch of elements style values by using those
elements name.

Here is my situation.


I have several colors swatch images (sample)

<img id='color1::mens::size40' name='color_swatch' src='...'
onclick='select(this)'/>
<img id='color2::mens::size40'...
Changing text size on different browsers from Javascript
Hi,

I want to change text sizes in Drupal, the existing modules for this don't
seem to work.

Basically for font-size'es xx-small, x-small, small, medium, large, x-large,
xx-large, is there a javascript variable that will allow me to increase or
decrease these sizes ?

It maybe...
Changing text in input text box
There has to be some way you can change the words you have put into a
<input type="text box because I have seen on-screen calculators that
can put your result into one. But I can't find the exact way among all
the entries in the lists of objects and other stuff. It seems to have
something...
Set iframe src without default.
I have a page with an iframe in it

<iframe src="blah.htm"></frame>

If javascript is enabled then at window.onload the source is changed
depending on what's found in the hash part of the URL. If javascript is
not enabled then users get to see 'blah.htm' - the default.

The issue that...
OnChange from iframe
I'm trying to call a function after a text field is changed from a
separate iframe. Like this:

<input type="text" name="pic" value="" onChange="alert('works')">
<iframe id="iframe_pic" src="whatever.cgi" width="150" height="35"></
iframe>

The iframe runs a function, then on success sends...