Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing iframe src with Javascript

I am trying to change an <iframe src=... > when someone clicks a radio button. For some reason my code is not working correctly and I am having trouble figuring out why. Here is what I have:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml">    <head>    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />    <title>Untitled 1</title>      <script>    function go(loc) {      document.getElementById('calendar').src = loc;    }    </script>  </head>    <body>    <iframe id="calendar" src="about:blank" width="1000" height="450" frameborder="0" scrolling="no"></iframe>      <form method="post">      <input name="calendarSelection" type="radio" onselect="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Day      <input name="calendarSelection" type="radio" onselect="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Week      <input name="calendarSelection" type="radio" onselect="go('http://calendar.zoho.com/embed/9a6054c98fd2ad4047021cff76fee38773c34a35234fa42d426b9510864356a68cabcad57cbbb1a0?title=Kevin_Calendar&type=1&l=en&tz=America/Los_Angeles&sh=[0,0]&v=1')" />Month    </form>    </body>    </html>
like image 276
shinjuo Avatar asked Sep 16 '10 19:09

shinjuo


People also ask

Does the src property of an iframe can be changed using JavaScript?

To change the iframe src attribute, we will be using a select drop down menu, and a button element to activate the function that will change the src attribute. Note that this is only for example purposes, instead of using onClick you may want to consider using event handlers in an external script.

How do I change iframe content dynamically?

You can use the following to change the src attribute of the iFrame: $("#content"). attr('src', 'http://mysite.com/newpage.html');

Can JavaScript interact with iframe?

On this page, two iframes interact with each other using JavaScript. First we show how one iframe can get references to the other iframe and the document inside it. Then we provide an example which demonstrates one iframe accessing and modifying the other's properties, objects, and content.


2 Answers

In this case, it's probably because you are using the wrong brackets here:

document.getElementById['calendar'].src = loc; 

should be

document.getElementById('calendar').src = loc; 
like image 69
Pekka Avatar answered Nov 11 '22 13:11

Pekka


Maybe this can be helpful... It's plain html - no javascript:

<p>Click on link bellow to change iframe content:</p>  <a href="http://www.bing.com" target="search_iframe">Bing</a> -  <a href="http://en.wikipedia.org" target="search_iframe">Wikipedia</a> -  <a href="http://google.com" target="search_iframe">Google</a> (not allowed in inframe)    <iframe src="http://en.wikipedia.org" width="100%" height="100%" name="search_iframe"></iframe>

By the way some sites do not allow you to open them in iframe (security reasons - clickjacking)

like image 29
inemanja Avatar answered Nov 11 '22 13:11

inemanja