iframe

admin

Administrator
Staff member
if you create an iframe, is there a way to change the src - dynamically change it to a different htm document?<br />
<br />
i have tried the obvious - document.iframeName.src and everything else i can think of.<br />
<br />
i am trying to swap the contents with a button.<!--content-->I think you need to use the frames array:<br />
<br />
<br />
HTML:<br />
<iframe name="iframe1" src=http://www.webdeveloper.com/forum/archive/index.php/"whatever.htm"></iframe><br />
<br />
JS:<br />
document.frames['frame1'].src = 'http://www.webdeveloper.com/forum/archive/index.php/whereever.htm';<br />
<br />
<br />
I think that's right; going from memory.<!--content-->Rick -<br />
<br />
In your example above i changed the js to:<br />
<br />
document.frames['iframe1'].src = 'http://www.webdeveloper.com/forum/archive/index.php/whereever.htm';<br />
<br />
to match the names (caught that because it generated an error).<br />
<br />
however, still no luck. seems to do nothing for me (src did not change - but no error generated). i will poke around some more with it. i am not familiar with the frames array.<br />
<br />
any additional thoughts?<!--content-->This might work. The user will have to have JavaScript though.<br />
<br />
Instead of <iframe src=http://www.webdeveloper.com/forum/archive/index.php/"whatever.html"> do:<br />
<br />
<script language="JavaScript" type="text/JavaScript"><br />
<br />
document.write("<iframe src=http://www.webdeveloper.com/forum/archive/index.php/\"");<br />
function pageOne() {<br />
document.write("page1.html");<br />
}<br />
<br />
function pageTwo() {<br />
document.write("page2.html");<br />
}<br />
document.write("\">");<br />
<br />
</script><br />
<body onLoad="pageOne();"><br />
<input type="button" value="page1.html" onClick="page2.html;"><br />
<br />
I don't know if that will work, but maybe it will. You might to do some tweaking of the code.<!--content-->got it:<br />
<br />
document.iframe1.location.replace("xxx.htm");<br />
<br />
thanks for all the input everyone.<br />
<br />
but i am still curious about the frames array. must do some research.<!--content-->Heh, I was just about to post this:<br />
<br />
<br />
document.frames['iframe1'].location.href<!--content-->Rick -<br />
<br />
either what you posted or what i posted works great with IE6. thanks for coming back.<br />
<br />
but on further testing, neither solution works with NS7.<br />
<br />
this cross-browser stuff is going to drive me nuts.<!--content-->Hopefully, someday, all the browsers will be as good as IE.:D :D :D<!--content-->IE the best, hmm that's a matter of opinion :p<br />
<br />
To get this to work in NS you probably need to use window.frames instead of document.<!--content-->can't get window. rather than document. to work in NS either.<br />
<br />
but thanks for the idea.<!--content-->Using window instead of document worked in both browsers for me ( tested in IE6 and NS7 ).<br />
<br />
document.frames['iframe1'].location.href = 'http://www.webdeveloper.com/forum/archive/index.php/some-page.html'<!--content-->thanks for coming back at me again. window. is working for me now as well in both browsers. i must have messed something up on my other test.<br />
<br />
thanks to all who pitched in. don't you love a happy ending!<!--content-->
 
Back
Top