Tuesday, October 25, 2011

How do change the iframe in a new page?

Currently I have two different pages with links. Both have the same links.

One page has an iframe however.



I can get the iframe to change via links on that page. On this page the iframes opens up with a page from another website.



From the page without the iframe, if you press a link it will go to the page with the iframe and change the iframe to anther page depending on the link.



This is a sample of the code that i currently have.



//Page with iframe



%26lt;script type=%26quot;text/javascript%26quot;%26gt;

function changeTo_test_link()

{

document.getElementById(%26quot;frame1%26quot;).src?br>
%26lt;/script%26gt;





%26lt;body%26gt;

%26lt;li%26gt;%26lt;a href=%26quot;javascript:changeTo_test_link()%26quot;%26gt;t?br>
%26lt;iframe scrolling=%26quot;no%26quot; src=%26quot;Home_test.html%26quot; id=%26quot;frame1%26quot; width=%26quot;850%26quot; height=%26quot;650%26quot; border=%26quot;0%26quot; frameborder=%26quot;0%26quot; framespacing=%26quot;0%26quot;%26gt;%26lt;/iframe%26gt;

%26lt;/body%26gt;







//Index page with no iframe



%26lt;body%26gt;

%26lt;li%26gt;%26lt;a href=%26quot;javascript:changeTo_test_link()%26quot;%26gt;t?br>
%26lt;/body%26gt;How do change the iframe in a new page?By my guess, the contents of the iframe are using a script similar to this to make sure that it opens as the top frame:



Code:

if (top.location != self.location)

{

top.location = self.location;

}





A workaround may be to disable JavaScript for that page, but that may result in the login page not working properly



Access

Either you access the iframe as an object and change its src or you access the iframe as a frame and change its location.href.



document.getElementById('iframe_id').s?= 'newpage.html';

frames['iframe_name'].location.href = 'newpage.html';

The frame syntax is slightly preferable because Opera 6 supports it but not the object syntax.

Accessing the iframe

So for a complete cross揵rowser experience you should give the iframe a name and use the



frames['testiframe'].location.href

syntax. As far as I know this always works

Generated iframes

When you generate an iframe through the W3C DOM the iframe is not immediately entered into the frames array, though, and the frames['testiframe'].location.href syntax will not work right away. The browser needs a little time before the iframe turns up in the array, time during which no script may run.



The document.getElementById('testiframe').sr?syntax works fine in all circumstances.



The target attribute of a link doesn't work either with generated iframes, except in Opera, even though I gave my generated iframe both a name and an id.



The lack of target support means that you must use JavaScript to change the content of a generated iframe, but since you need JavaScript anyway to generate it in the first place, I don't see this as much of a problem.



Example

%26lt;iframe src=%26quot;iframe_page1.html%26quot;

id=%26quot;testiframe2%26quot;%26gt;%26lt;/iframe%26gt;