简体   繁体   中英

How do I refresh a link inside an IFrame?

I have a div that works pretty much like a button. When clicked, it reloads an IFrame. However, in this IFrame, there are some hyperlinks you can visit. When on one of the links inside the IFrame, if you click the refresh div, the IFrame refreshes to the original page, not the hyperlink one.

Here's an example:

<!DOCTYPE html>
<html>
<body>
    <a href="#" onclick="refreshIframe()">Refresh</a><br/>
    <iframe src="https://en.wikipedia.org/wiki/Main_Page" width="600" height="400" name="page"></iframe>
    <script>
        function refreshIframe() {
            var ifr = document.getElementsByName('page')[0];
            ifr.src = ifr.src;
        }
    </script>
</body>

This reloads to the original IFrame link. I want it to refresh the page you are on, but I have no idea how this can be done.

You'll want to reload the current location of the iframe, since that's where it's stored, not the src, which hasn't been changed. It'll look like:

window.frames['page'].location.href.reload()

You can only get the url for the page loaded within the iframe if the content of the iframe, and the referencing javascript code (your parent page), are served from the same domain. This is for security reasons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM