Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change iframe src by clicking a link

I have an iframe that looks like this:

<iframe src="http://www.google.com" height=1000 width="500" id="myiframe"></iframe>

I want to create a link so that when I click on it, the iframe on the page changes. How can I do that using jQuery? Is it related to jQuery attr?

like image 692
chris Avatar asked Sep 24 '09 18:09

chris


People also ask

Can you modify iframe content?

When you can communicate with the code inside an iFrame, you can make any change you want to the code within that iFrame.

How do I open a link in an iframe?

You can embed an iframe in a page that is inside another iframe on another web page. When you set the target attribute to _parent, the link will open in the web page that is holding the iframe. In most situations with iframes, this target will open links in the same way that the _parent target does.

Can you iframe a specific part of page?

Set the iframe to the appropriate width and height and set the scrolling attribute to "no". If the area you want is not in the top-left portion of the page, you can scroll the content to the appropriate area.


1 Answers

You don't need jQuery for that. You don't even need JavaScript for that.

Give your iframe a name, and target your anchors to point to it:

<a href="foo.html" target="myiframe">Foo</a>
<a href="bar.html" target="myiframe">Bar</a>
<a href="baz.html" target="myiframe">Baz</a>

<iframe name="myiframe"></iframe>

This degrades nicely for people who have JavaScript turned off.

like image 107
Roatin Marth Avatar answered Oct 19 '22 20:10

Roatin Marth