get parent.location.url - iframe - from child to parent

后端 未结 7 2438
时光说笑
时光说笑 2020-11-30 03:12

I got a page that is displayed within an iframe.

I need to get the parent.location.url with js from that page (child page).

Both sites are in different domai

相关标签:
7条回答
  • 2020-11-30 03:37

    What you're trying to do isn't possible per se. Unless you have control over the parent page's calling of the child page. If you do, then you can call the child page with a get statement in the URL of the parent page. So if you parent page is http://parentpage.com then here's how you should call the iFrame.

    <iframe src='http://childpage.com/?parent=http://parentpage.com' />
    

    Then all you have to do in the child page to get the reference is get the string with

    window.location.search //not sure of browser compatibility with this
    

    and extract the parent page's URL by grabbing it out of that string. I'd give more details, but I'm not even sure about the first question I had.

    0 讨论(0)
  • 2020-11-30 03:44

    You'll be able to find it like this, and all browsers should be able to support it:

    query = window.parent.location.search.substring(1);
    

    You shouldn't get permission errors with this, but I may be wrong.

    0 讨论(0)
  • 2020-11-30 03:56

    Have you tried something like:

    alert(window.top.location.href);
    
    0 讨论(0)
  • 2020-11-30 03:58

    Try:

    var pathname = window.parent.location.href
    
    0 讨论(0)
  • 2020-11-30 03:59

    try this:

    var referrer = document.referrer;
    alert(referrer);
    
    0 讨论(0)
  • 2020-11-30 03:59

    It will only work if the parent page and the iframe are on the same domain. If they are not on the same domain you can try passing the location through the src, like Madison Williams suggested.

    That is probably why you are getting "Permission denied..."

    0 讨论(0)
提交回复
热议问题