Open link in iframe

JavascriptHtmlIframeTarget

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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.

Solution 3 - Javascript

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

Solution 4 - Javascript

<a href="YOUR_URL" target="_YOUR_IFRAME_NAME">LINK NAME</a>

Solution 5 - Javascript

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>

Solution 6 - Javascript

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.

Solution 7 - Javascript

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmoomooView Question on Stackoverflow
Solution 1 - JavascriptAnthonyView Answer on Stackoverflow
Solution 2 - JavascriptjthompsonView Answer on Stackoverflow
Solution 3 - JavascriptOspiteView Answer on Stackoverflow
Solution 4 - JavascriptAlix AxelView Answer on Stackoverflow
Solution 5 - JavascriptbobView Answer on Stackoverflow
Solution 6 - Javascriptjohn smithView Answer on Stackoverflow
Solution 7 - JavascriptKarthik EyanView Answer on Stackoverflow