0

Have small requirement, Have one iframe and followed by 3 buttons. Once we click on button1 the url should be displayed in the iframe, same as second button. Here is my code,Could you please help.

function testCoverage(){    
     window.frames['idIfrm'].document.location.href = 'http://google.com';
    }
function testCoverage1(){   
    window.frames['idIfrm'].document.location.href = 'http://yahoo.com';
}

Here is JsFiddle

3 Answers 3

1

Please try this code, it is working fine in my end,

<div>
    <button><a href="http://www.computerhope.com/banners/banner.gif" target="iframe_a">Banner1</a></button>
    <button><a href="http://www.computerhope.com/banners/banner2.gif" target="iframe_a">Banner2</a></button>
    <button><a href="http://www.computerhope.com/banners/banner3.gif" target="iframe_a" onclick="closeWindow();">Banner3</a></button>
    <div>       
    <iframe src="http://www.computerhope.com/banners/banner3.gif" name="iframe_a" width="900" height="350px" id="iframe" frameborder="0" scrolling="no" style="margin-left:15px;margin-bottom:15px;border:none;">
    </iframe>
    </div>
 </div>
Sign up to request clarification or add additional context in comments.

Comments

1

You should use the src attribute. your code should look like this

    function testCoverage(){
       document.getElementById['idIfrm'].src = 'http://google.com';
    }
    function testCoverage1(){   
       document.getElementById['idIfrm'].src = 'http://yahoo.com';
    }

Comments

1

Well your fiddle does not run since it is set up to run the JavaScript onload and not in the head. Change the dropdown on the top left.

enter image description here

Second issue, you should be setting the source of the iframe, not the document location.

window.frames['idIfrm'].src = "foo.html";

Third issue which you can not do anything about is Google and Yahoo can not be put into the iframe. You will get the error

Refused to display 'https://www.google.com/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.