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

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 domains.
I'm trying:
alert(parent.location.url)
But I get this error:
Permission denied for http://parentdomain to get property Location.URL from http://childdomain.
???? Is it possible?

try this:
var referrer = document.referrer;
alert(referrer);

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.

Have you tried something like:
alert(window.top.location.href);

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.

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..."

Use postMessage to pass data back and forth between the windows. It's a little klunky, but required when having windows communicate across origins.

Try:
var pathname = window.parent.location.href

alert(window.top.location.href);
This works fine. To get the source URL from child (iframe) to parent.

Related

Change window location href in subdomain iframe

I have a.example.com and b.example.com;
When I put b.example.com/foo as iframe in a.example.com:
<iframe src='b.example.com/foo'></iframe>`
the window.location.href value accessible from b.example.com is ... a.example.com/foo (??)...
The problem is - I use an external library in b.example.com/foo that checks for window.location.href and it gets incorrect information.
For this scenario it appears to me that I should iframe /foo content on a completely different domain like:
anotherexample.com/foo, where the window.location.href would actually point to anotherexample.com/foo.
How can I force window object in iframe to actually use its subdomain instead of parent domain with added parameter?
Are you sure you are using your script in the right subdomain? it seems your external library is accessing parent window using window.parent.location, here is a code to check if a page is loaded directly or within another page:
var is_loaded_directly = (window.location == window.parent.location);
Since this code works, something is wrong on your setup or library.
Note: maybe you can solve your issue by checking document.location instead of window.location.

Get the url from frame redirection

I did a frame redirection to redirect subdomain.domain1.com to domain2.com. It works well ie I got "subdomain.domain1.com" in the address bar and the domain2.com is well displayed. However, I try to get the url in js code.
console.log(window.location.href);
This line show me "domain2.com" but it would be "subdomain.domain1.com". How to get that ?
window refers to the current page.
If you want the URL of the parent frame then you would need to go to window.parent.
At that point you'll run into a cross-origin error. If you controlled the parent then you could use postMessage to pass the data through.
Since you are using a frame redirection, that seems unlikely though. Generally this is one of many limitations with frame based redirection that mean you should generally avoid that approach in the first place.

.find().html() in jquery always returning null when used inside iframe

I am trying to access data of the html document(contains a table) that I am opening in iframe.
I want to access table data.
//#we is id of a <td> #iframe is id of iframe
var q=$('#iframe').find('#we').html();
document.write(q);
But when i use this i always get a null value.help.
Make sure that the iframe is on the same domain, otherwise you can't get access to its internals. That would be cross-site scripting.
None of the browsers would allowing accessing iframe which is from a different domain than your site. It is a serious security breach. Thus if you are loading iframe from different domain, no matter what you do, you can't access it.
You might not like the answer, but it is the fact.
If your iframe is in fact on the same domain - you can use the following.
var q = $('#iframe').contents().find('#we').html();
Please try this:
var body = $('#iframe').contents().find('body');
or this can also be helpful:
$(editFrame).contents().find("html").html();
That should return all the html in the iframe for you. Or you can use "body" or "head" instead of "html" to get those sections too.

Update URL from inside an iFrame

is it possible for javascript inside an iFrame to update the URL (hash) of the parent page (and retrieve it)
Does it have any permissions?
To further explain, I have no hosting for this domain, I can only set up an Iframe. I also cannot use a DNS config to get that page to display because of limitations of my hoster.
I also cannot transfer the domain to them to make that work because my clients wants to keep control of the domain.
Thank you for your help!
If the <iframe> page is within the same domain, probably yes. Otherwise you don't get access to the parent page due to cross-domain restrictions.
You can change the URL of the parent page though:
top.location.href = 'http://www.example.com';
due to security constraints you will not be able to access properties of the parent window IF the domain,port or protocol is different than the one in the iframe.
To be short, the answer is NO.
Your script works only inside the context of that iframe.
If you try for example,
var loc = document.location;
you will see what I mean.
One solution is that when you give the other side your iframe, you should add a script in witch you can do whatever you want, because it runs on their domain.
Maybe dynamically create the source of your iframe and stuff.

Javascript get element from opened window

I need to open a new window and return an element contained in it.
Say we have page A and page B, I want:
open B from A
get the element interested in B
return that element to A
I tried to do so in this manner, but it doesn't work:
var newwindow = window.open("http://www.example.com");
var elem = newwindow.document.getElementById('my-id').value;
Where am I wrong? Has anyone some advice to me?
Since you are using an absolute URI, I'm going to assume that you are trying to grab data from a different website. You'll therefore be blocked by the same origin policy.
If that isn't the case, then you're probably hitting a race condition by trying to read the content of the document before it has finished loading.
It would be a lot easier to help if you provided the error messages that your browser is almost certainly logging to its JS console.
If the new window has the same protocol, domain and port, your code should work. If it's on another domain, you can't do this for security reasons.
If you control both pages, you could use window.postMessage.

Categories