How can I change content of an iFrame dynamically in iWeb?

My question is related to another issue I had in this thread: https://discussions.apple.com/thread/3045473


I wanted to have an iFrame in a page to have as its content another page in my iWeb site. The issue I had was solved by using the proper relative URL paths. I think I may have a similar problem here.


What I really wanted was a clean submenu bar for one of my pages. In place of a submenu, I decided to use a few javascript buttons that dynamically change the content of an iFrame. I'm using HTML snippets for this, one for each of the buttons, and another for the iFrame whose content will be changing.


For some reason, however, I can't find a way to dynamically change the content of the iFrame. When I click on the button, the page gets reloaded to a page whose only content is the button. I've tried different ways of getting the iFrame by id and changing its source. I know the function gets called because I tested it with a dummy alert. Here's an example of something I tried...


Here's the code for the iFrame:



<iframe src = "../TestPage.html" id="contentFrame" width="650px" height="600px" frameborder="0" scrolling="no" allowTransparency="true"></iframe>



Here's the code for the button:


<script type="text/javascript">

function changeContent()

{

document.getElementById("contentFrame").src = "http://www.apple.com";

}

</script>

</head>



<a href="#" onclick="changeContent()" >Submenu Item 1</a>


I think iWeb is somehow interfering with how I'm getting the iFrame by its id. How can I get this to work?

iWeb '08, Mac OS X (10.6.7)

Posted on May 8, 2011 7:33 PM

Reply
5 replies

May 8, 2011 11:55 PM in response to analiai

First,this is the proper <iframe> code (no px) :


<iframe src="http://url.to.file.html" frameborder="0" width="500" height="500" scrolling="no" allowTransparency="true"></iframe>


For your purpose you have to add a name or an id to the <iframe>


<iframe id="contentFrame" src="http://url.to.file.html" frameborder="0" width="500" height="500" scrolling="no" allowTransparency="true"></iframe>


And there's no need for JavaScript. HTML provides enough code to link to another frame :


<a href="url.to.page.html" target="contentFrame">Click link</a>


Here's how I do it :


http://www.wyodor.net/iFrame/

May 14, 2011 11:11 AM in response to Wyodor

I already had an id for my iFrame, and preferred Javascript because I needed additional effects (like highlighting the option I'm currently viewing - how can I do that with HTML?). I'm not as experienced with HTML, so it's easier for me to know how to do that using Javascript.

I tried your alternative, and got it to work by changing the relative URL path. The link that changed the content of the iFrame had to go one directory up to find the page I wanted that was part of my site:


<a href="../PageIWanted.html" target="contentFrame">Link Title</a>


I'm curious to know why the Javascript option doesn't work...

May 17, 2011 12:22 PM in response to Wyodor

With your help I solved the original iFrame problem, but have another, worse (more unpredictable) issue.


Similarly to your site, I have media content in my iFrames. I started testing the functionality of the iFrame loading the other page sources with a menu similar to yours on the left side. However, when there is both text and a video to be loaded, sometimes the frame loads only the video and not the text. I miraculously figured out that sometimes when I scroll down/up the page, the text appears! Have you experienced something like this?


Also, on Firefox the videos usually don't play... this is becoming rather frustrating given the limitations of iWeb.


Any help is appreciated.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How can I change content of an iFrame dynamically in iWeb?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.