Get DOM content of cross-domain iframe
You can't. XSS protection. Cross site contents can not be read by javascript. No major browser will allow you that. I'm sorry, but this is a design flaw, you should drop the idea.
EDIT
Note that if you have editing access to the website loaded into the iframe, you can use postMessage (also see the browser compatibility)
how to resolve iframe cross domain issue
You need control over the domain you want to embed to remove/amend its CORS policy.
If the domain has explicitly blocked Cross-Origin requests, there's nothing you can do about it.
This is used to avoid anyone hijacking any site you want (you could have a full screen Google in an iframe running with your ads on top on bettergoogle.com, things like that).
This page will give you more insights on Cross-Origin
Access cross-domain iframe elements using JavaScript
You should first read about the browser's same-origin policy. This prevents javascript from a frame or window or iframe in one origin from accessing the content or scripts in another frame in a different origin. So, if your iframe is not the same origin as the page of your script, then it cannot directly access the iframe's contents.
If you have one of the latest browsers and you code both frames to cooperate (which means you have to control the javascript code in both frames), there is a new feature called window messaging that can be used to pass information between frames from different origins.
Get iframes complete url which is from another domain
You can use window.postMessage
API to communicate between frames by calling postMessage
on any window
object. This works for cross-domain messaging, as long as the receiving frame has a message
event listener.
postMessage Usage:
otherWindow.postMessage(message, targetOrigin);
//e.g.
yourIFrame.contentWindow.postMessage({curURL: location.href}, "http://www.your2ndDomain.com");
//or
top.postMessage({curURL: location.href}, "http://www.your1stDomain.com");
To listen for messages:
window.addEventListener("message", function(e){
//You must check your origin!
if(event.origin !== "http://www.your1stDomain.com")
return;
//Get the data
var data = event.data;
alert(data.curURL);
}, false);
Cross-domain JavaScript iFrame-parent access blocked
After doing lots of searching around, I came across this:
http://www.codeproject.com/Tips/585663/Communication-with-Cross-Domain-IFrame-A-Cross-Bro
I actually tested the method (using my own short piece of code) and it seemed to work on Chrome, Firefox and IE. Now I'm gonna try the "real" implementation...
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.
Related Topics
How to Download File from Server
How to Stop a Window.Setinterval in JavaScript
How to Convert String to Number According to Locale (Opposite of .Tolocalestring)
Jasmine: Async Callback Was Not Invoked Within Timeout Specified by Jasmine.Default_Timeout_Interval
About Closure, Lexicalenvironment and Gc
Chrome Extension: Port Error: Could Not Establish Connection. Receiving End Does Not Exist
React - Preventing Form Submission
Why Does Isnan(" ") (String with Spaces) Equal False
Javascript: Get Argument Value and Name of Passed Variable
How to Highlight the Text of the Dom Range Object
Why Were Es5 Object Methods Not Added to Object.Prototype
Secure Random Numbers in JavaScript
Detecting Line-Breaks with Jquery
JavaScript - How to Get the Url of Script Being Called