58

I have a Webpage with an iframe in it.

I want the links, when clicked, to open them in the frame.

I tried <a href="" target="nameofframe">link1</a> but it didnt' work?

How can web links be displayed in a frame?

dazzafact
  • 2,570
  • 3
  • 30
  • 49

7 Answers7

63

Assuming the iFrame has a name attribute of "myIframe":

<a href="http://www.google.com" target="myIframe">Link Text</a> 

You can also accomplish this with the use of Javascript. The iFrame has a src attribute which specifies the location it shows. As such, it's a simple matter of binding the click of a link to changing that src attribute.

Anthony
  • 7,086
  • 1
  • 21
  • 22
46

Try this:

<iframe name="iframe1" src="target.html"></iframe>

<a href="link.html" target="iframe1">link</a>

The "target" attribute should open in the iframe.

honk31
  • 3,895
  • 3
  • 31
  • 30
jthompson
  • 7,178
  • 2
  • 34
  • 33
9

Use attribute name.

Here you can find the solution ("Use iframe as a Target for a Link"): http://www.w3schools.com/html/html_iframe.asp

Ospite
  • 137
  • 2
  • 10
5
<a href="YOUR_URL" target="_YOUR_IFRAME_NAME">LINK NAME</a>
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
2

Because the target of the link matches the name of the iframe, the link will open in the iframe. Try this:

<iframe src="http://stackoverflow.com/" name="iframe_a">
<p>Your browser does not support iframes.</p>
</iframe>

<a href="http://www.cnn.com" target="iframe_a">www.cnn.com</a>
bob
  • 39
  • 4
1

I had this problem in a project this morning. Make sure you specify the base tag in the head section.

It should be like this:

<head>
<base target="name_of_iframe">
</head>

That way when you click a link on the page it will open up inside of the iframe by default.

Hope that helped.

Matt Cain
  • 5,638
  • 3
  • 36
  • 45
john smith
  • 19
  • 1
1

Well, there's an alternate way! You can use a button instead of hyperlink. Hence, when the button is clicked the web page specified in "name_of_webpage" is opened in the target frame named "name_of_iframe". It works for me!

<form method="post" action="name_of_webpage" target="name_of_iframe">
<input type="submit" value="any_name_you_want" />
</form>
<iframe name="name_of_iframe"></iframe>