Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Linking to a different page from inside an iframe

thesoundsmith

Active Coder
I have a page in a WordPress site that opens into an IFrame to accommodate a music player I wrote years ago. It all works fine excerpt when trying to close the window. .The anchor or onclick is inside the iframe, so the new window (front page) opens in the same iframe that held the music player and the URL is still the radio page. I need the "dashradio" window to close and the front page to reopen
Here's the line:
HTML:
<a id="btn6"  title="Close DashRadio" href="https://www.davidkempton.com"></a>

The iframe in the WordPress page looks like this:
HTML:
<iframe src="https://www.davidkempton.com/radioREV.htm" name="dashRadio" height="870px" width="100%" title="DashRadio" align="middle" scrolling="no" style="border:none;"></iframe>

The page in operation: The DashRadio page - if you click on a Menu item, it works as expected, ONLY when you select the red 'Close' button does this occur. I COULD simply remove the button, I suppose, but it is part of the image...
Thanks.
 
Solution
Hi, and thanks for the reply.
No, that is the problem I need to rectify. I need it to react exactly as if I had hit the leftmost menu 'Home' button - close itself and open the home screen.
The easiest way to do this would be to add the attribute target="_top" to that anchor tag, like this:
HTML:
<a id="btn6"  title="Close DashRadio" href="https://www.davidkempton.com" target="_top"></a>
This ensures that the top most iframe, in this case it would be the actual page itself, gets redirected to the link in the href attribute. Otherwise, if you really need to do it in javascript, you can just add these few lines to your existing js code:

JavaScript:
var closeBtn = document.querySelector('a#btn6');
closeBtn.addEventListener('click', (event)...
I have a page in a WordPress site that opens into an IFrame to accommodate a music player I wrote years ago. It all works fine excerpt when trying to close the window. .The anchor or onclick is inside the iframe, so the new window (front page) opens in the same iframe that held the music player and the URL is still the radio page. I need the "dashradio" window to close and the front page to reopen
Here's the line:
HTML:
<a id="btn6"  title="Close DashRadio" href="https://www.davidkempton.com"></a>

The iframe in the WordPress page looks like this:
HTML:
<iframe src="https://www.davidkempton.com/radioREV.htm" name="dashRadio" height="870px" width="100%" title="DashRadio" align="middle" scrolling="no" style="border:none;"></iframe>

The page in operation: The DashRadio page - if you click on a Menu item, it works as expected, ONLY when you select the red 'Close' button does this occur. I COULD simply remove the button, I suppose, but it is part of the image...
Thanks.
I have a page in a WordPress site that opens into an IFrame to accommodate a music player I wrote years ago. It all works fine excerpt when trying to close the window. .The anchor or onclick is inside the iframe, so the new window (front page) opens in the same iframe that held the music player and the URL is still the radio page. I need the "dashradio" window to close and the front page to reopen
Here's the line:
HTML:
<a id="btn6"  title="Close DashRadio" href="https://www.davidkempton.com"></a>

The iframe in the WordPress page looks like this:
HTML:
<iframe src="https://www.davidkempton.com/radioREV.htm" name="dashRadio" height="870px" width="100%" title="DashRadio" align="middle" scrolling="no" style="border:none;"></iframe>

The page in operation: The DashRadio page - if you click on a Menu item, it works as expected, ONLY when you select the red 'Close' button does this occur. I COULD simply remove the button, I suppose, but it is part of the image...
Thanks.
Hi there,

So I was testing your site, and when I click on the "close" button, the main page opens up inside of the iframe itself...is that the intended functionality?
 
Hi, and thanks for the reply.
No, that is the problem I need to rectify. I need it to react exactly as if I had hit the leftmost menu 'Home' button - close itself and open the home screen.
The easiest way to do this would be to add the attribute target="_top" to that anchor tag, like this:
HTML:
<a id="btn6"  title="Close DashRadio" href="https://www.davidkempton.com" target="_top"></a>
This ensures that the top most iframe, in this case it would be the actual page itself, gets redirected to the link in the href attribute. Otherwise, if you really need to do it in javascript, you can just add these few lines to your existing js code:

JavaScript:
var closeBtn = document.querySelector('a#btn6');
closeBtn.addEventListener('click', (event) => {
    event.preventDefault();
    window.top.location.href = '/';
});

The code above gets the button from DOM, sets up an event listener that listens for a click event. Once the button is clicked, the default functionality of the page redirecting inside the iframe is disabled and the page itself gets redirected to main page, "/", with 'window.top.location.href'
 
Last edited:
Solution

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom