Access parent URL from iframe
You're correct. Subdomains are still considered separate domains when using iframes. It's possible to pass messages using postMessage(...)
, but other JS APIs are intentionally made inaccessible.
It's also still possible to get the URL depending on the context. See other answers for more details.
How to get the parent window URL from Iframe?
Try this inside the iframe. It will alert the parent window's location URL.
alert(document.referrer);
Is there a way to get the parent URL from an Iframe's content?
top.location.href
But that will only work if both pages (the iframe and the main page) are being served from the same domain.
get the parent url of iframe in PHP
Try with:
parent.location.href;
or
parent.document.URL
How to get variable from parent url or parent iframe?
As Eugen Rieck is trying to find out, content can only communicate through iframes when they originate from the same domain, otherwise you will run into the same origin policy.
To communicate with a document in an iframe you can use
var frame=window.document.getElementById('yourFrame');
var doc=frame.contentWindow.document;
You can then communicate with the elements of the document in the iframe.
I would include the variable that you are trying to "grab" in example.php as a hidden text input. You can do this by running a querystring:
var queryString = new Object;
var qstr = window.location.search.substring(1);
var params = qstr.split('&')
for (var 1=0; i<params.length; i++) {
var pair = params[i].split('=');
queryString[pair[0]]=pair[1];
var variable = queryString['variable'] //find the value of variable
document.write("<input style='visibility:hidden' id='variable' type='text' value='"+ variable +"'></input>");
Once that is in the iframed document, pull it back out in the parent by using:
var variable = doc.getElementById('variable').value;
and bob's your uncle.
Let me know if this helps....
----EDIT----
Alternatively, if you were to write this in a function in the parent page, to invoke it from the child page you could use:
function callParentFunction() {
parent.getVariable();
return false;
}
You could run an onload to call this or something
Related Topics
Why Is My .Sticky-Top Class Not Working in Bootstrap 4
Why Are CSS-Styles Not Inherited by HTML Form Fields
How to Change an HTML Output of Wcf Service with My Own Content
Automatic Retina Images for Web Sites
After Pseudo Element Not Appearing in Code
Vertically-Aligned Inline-Block Element Not Perfectly Centered Within Container
Link External CSS File Only for Specific Div
Why Td Width Is Not Working or Not Followed
Why People Minify Assets and Not The HTML
Elements in a Flex Container Are Not Wrapping
Equal Height Flexbox Columns in Chrome
Multiple Background Images in One Div
Aligning Elements Left, Center and Right in Flexbox