Search code examples
javascripthtmlframes

Change the URL of the frame using Javascript


I have got 2 frames in my page frame1 and frame2. Frame 2 is displaying a html page called rightpage.html. So when I click the rightpage.html on frame2 it needs to change the URL of frame1 URL to toppage.html.

<html>
    <frameset cols='45%,55%'>
        <frame src='leftpage.html' name='frame1' id='frame1' frameborder='0'>
        <frame src='rightpage.html' name='frame2' id='frame2' frameborder='0'>
    </frameset>
</html>

Could you please help?

Thanks in advance.


Solution

  • If you want to use a JavaScript solution, you can use the following snippet:

    parent.frame1.location = 'toppage.html'
    

    You may also use the target attribute of an anchor. For example

    <a href="toppage.html" target="frame1">Link text</a>
    

    PS: Using frames is not really advisable for modern websites or web applications, I hope you have a good reason for it.