简体   繁体   中英

How to check if the URL of another frame on my site contains a given string?

My site has a parent page with 2 iframes on it. Currently I have it all running how I would like, with the exception that using the browser back button only changes the content of 1 of the 2 iframes. I'm trying to make a check inside the child frames so that the correct content page is loaded in the sister iframe. My iframes are leftiframe and rightiframe.

This is the code I have at the moment (not working):

<script type="text/javascript">
window.onload = function() {
  if(/iframe1.html/.test(parent.leftiframe.location.href))
  {
  }
  else
  {
   parent.leftiframe.location.href="iframe1.html";
  }
}
</script>

If I change it so that it is reading the string from within the current window, like so:

  if(/iframe2.html/.test(window.location.href))

then it has no problems reading the URL and will change the URL of the sister iframe without issue. Why does parent.leftiframe not work in place of window?

Thank you!

Try using this instead:

<script type="text/javascript">
    window.onload = function() {
        if(/iframe1.html/.test(parent.leftiframe.contentWindow.location.href))
        {
        }
        else
        {
            parent.leftiframe.contentWindow.location.href="iframe1.html";
        }
    }
</script>

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