Apr 27, 2010 9:29 am
Joe NineI 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 KinseyOn 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.
> 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 NineSean Kinsey wrote:
> On Apr 27, 11:29 am, Joe Nine <j...@yahoo.com> wrote:
> 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 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.
> >> 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 CornfordOn 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.
>>> 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.
>>> 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).
>> (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.
>>> 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;'
> '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
- Getting Value Without Changing HTML
- Opera 11 and changing input value
- JQuery changing document with?
- Changing letters in boxes
- Changing styles values by Element Name
- Changing text size on different browsers from Javascript
- Changing text in input text box
- Set iframe src without default.
- OnChange from iframe