Javascript - How can I get perfect window object from another server? [duplicate] - javascript

I open a website in a new tab through javascript by writing the following code in the browser console:
var win = window.open("http://staging.redefinewebs.in/wildgoose-const/wp-admin/post-new.php", "mywin", '');
Now I want to add text in a field in the newly opened tab. But for that I need the access to win.document. When I write win.document in the console I get the following error:
Error: Permission denied to access property "document"
This error doesn't appear if I open other websites in new tab. So,
How to get access of document object of a window opened in a new tab with window.open?

You cannot access a child window's DOM, if it violates the Same Origin Policy.
You can access the DOM of a child window, only if the following three conditions are satisfied.
Both windows have same protocol (http/https)
Both windows have same host (google.com and news.google.com are different)
Both windows have same port (google.com:80 and google.com:443 are different)

How to get access of document object of a window opened in a new tab with window.open?
If the window is opening a document from a different origin, you don't; cross-origin access is denied by the browser because of the Same Origin Policy. From the error in your question, that would seem to be the case here.
If the window contains a document from the same origin, you can access it as you've shown; but note that it may still be loading immediately after you call window.open and you might need to wait for it to finish doing so, perhaps with the DOMContentLoaded event.

Related

How to get access of document object of a window opened with window.open?

I open a website in a new tab through javascript by writing the following code in the browser console:
var win = window.open("http://staging.redefinewebs.in/wildgoose-const/wp-admin/post-new.php", "mywin", '');
Now I want to add text in a field in the newly opened tab. But for that I need the access to win.document. When I write win.document in the console I get the following error:
Error: Permission denied to access property "document"
This error doesn't appear if I open other websites in new tab. So,
How to get access of document object of a window opened in a new tab with window.open?
You cannot access a child window's DOM, if it violates the Same Origin Policy.
You can access the DOM of a child window, only if the following three conditions are satisfied.
Both windows have same protocol (http/https)
Both windows have same host (google.com and news.google.com are different)
Both windows have same port (google.com:80 and google.com:443 are different)
How to get access of document object of a window opened in a new tab with window.open?
If the window is opening a document from a different origin, you don't; cross-origin access is denied by the browser because of the Same Origin Policy. From the error in your question, that would seem to be the case here.
If the window contains a document from the same origin, you can access it as you've shown; but note that it may still be loading immediately after you call window.open and you might need to wait for it to finish doing so, perhaps with the DOMContentLoaded event.

Javascript access denied - can't access opener after parent is no longer used in an iframe

This is not related to the domain like many other similar posts with the same error message.
Here is my set up:
<iframe src='somepage' />
user can open a window popup from within somepage and window.open is used. Assume my popup remain open and a new source page 'secondPage' is swapped into the same iframe. My popup can still its 'opener' but whenever i try to access the opener, it will throw an access denied scripting error.
so my question is, how can i either access it or check to see if it closed so i dont try to access it? i tried to check if window.opener.closed and apparently is returning me a false.....
Your opener will have a property called closed which you can check to see if it's has been closed (or navigated away). If it's closed, checking any other property on the opener will throw an error because the window object has been destroyed.
I just noticed that you knew about the closed property. I've used this before and I never had problems.
What browser are you using? Post the code you're running. If all fails, use a try catch to detect that an error occurred while accessing the opener.
IE IFRAME BUG
What you've described is listed as bug in a comment on
http://msdn.microsoft.com/en-us/library/ms533574(v=VS.85).aspx

Chrome iframe's location.href is undefined

I'm working over the same domain giving the ability for a user to navigate within the iframe on the page. I'd like to handle the onload of the iframe in Chrome and to process the actual href the user navigated to. When I'm trying to access
document.getElementById("contentFrame").contentWindow.location.href
it says undefined. Debugger tells that location is an object, but I cannot figure out what property to use.
any guess?
Chrome v15
If you really are on the same domain, use the same protocol and port it should work. Here's an example: http://jsfiddle.net/csVcL/1/
Maybe you're on different subdomains, in which case you need to use document.domain
If you are not in the same domain, you are not allowed to access the href or any other properties on the remote window.

Getting the Document Location from another Window showing another Domain

I am trying to get window A (on our domain) open window B (another domain, like YouTube) and I want to show the current document location of window B in window A.
As an example, here is some of the code I am using:
var popup;
var popupRelay;
function findPopupURL(){
var loc=popup.document.location.href
$('#popuploc').html(loc);
}
function clickPopupLink(){
var windowProperties='height:500,width:1000,location:1,toolbars:0';//etc etc
popup=window.open('http://www.youtube.com/','popup',windowProperties);
popupRelay=setInterval(findPopupURL,1000);
}
This code generates this error however:
Error: Permission denied to access property 'document'
Unfortunately the work arounds I keep finding requires me to put code on both domains, which is obviously not possible if Im opening somewhere like YouTube.
What I want to know is, is there a way of getting the document location from another window showing another domain? Can it be done at all? And if so, how?
No, it is not possible to spy on what people do on other domains. That is exactly why the Same Origin Policy exists.

Unable to access data from a child window with https url

I have a simple html file. Using javascript I am opening a https link as a child window.
However, if I try to get any data say
var handler = window.open('https://abc.com','newWindow');
var title = handler.window.document.title;
Then it throws an exception saying
Permission denied for http://localhost to get property Window.document from https://abc.com
Can anyone help me out with this.
I don't really know if we are allowed to get data like that or not.
Thanks & Best Regards.
You are not allowed to access data in this way.
If you are a page on hostname A, you can open windows/frames on hostname B, but cannot in any way interact with them - for security reasons. Otherwise, I could open gmail in an iframe and find out your email address from the window title.
This restriction only applies if window A and window B have different hostnames (this includes protocol, host and port). So if you open iframes/windows on your own domain, this is not a problem.

Categories

Resources