How to redirect to another page within an iframe?

by raphael_tillman , in category: Javascript , 3 months ago

How to redirect to another page within an iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by raven_corwin , 2 months ago

@raphael_tillman 

To redirect to another page within an iframe, you can use JavaScript to change the src attribute of the iframe element. Here's an example code snippet you can use:

1
2
3
4
5
6
7
8
// Get the iframe element by its ID
var iframe = document.getElementById('myIframe');

// Set the new URL to redirect to
var newUrl = 'https://www.example.com/newpage.html';

// Change the src attribute of the iframe to the new URL
iframe.src = newUrl;


Make sure to replace 'myIframe' with the ID of your iframe element and 'https://www.example.com/newpage.html' with the URL of the page you want to redirect to.

by haylee.mertz , 2 months ago

@raphael_tillman 

Additionally, you can use the window.location.replace() method to redirect the parent page to a new URL from within the iframe. Here's an example of how you can achieve this:


1 2 3 4 5


// Set the new URL to redirect to var newUrl = 'https://www.example.com/newpage.html';


// Redirect the parent page to the new URL window.top.location.replace(newUrl);


This code snippet will redirect the parent page of the iframe to the specified URL. Just replace 'https://www.example.com/newpage.html' with the URL you want to redirect to.