Access parent URL from iframe

后端 未结 16 1543
野性不改
野性不改 2020-11-22 10:00

Okay, I have a page on and on this page I have an iframe. What I need to do is on the iframe page, find out what the URL of the main page is.

I have searched aroun

16条回答
  •  一整个雨季
    2020-11-22 10:32

    I just discovered a workaround for this problem that is so simple, and yet I haven't found any discussions anywhere that mention it. It does require control of the parent frame.

    In your iFrame, say you want this iframe: src="http://www.example.com/mypage.php"

    Well, instead of HTML to specify the iframe, use a javascript to build the HTML for your iframe, get the parent url through javascript "at build time", and send it as a url GET parameter in the querystring of your src target, like so:

    
    

    Then, find yourself a javascript url parsing function that parses the url string to get the url variable you are after, in this case it's "url".

    I found a great url string parser here: http://www.netlobo.com/url_query_string_javascript.html

提交回复
热议问题