Wednesday, October 01, 2008

Javascript - Access Denied When Calling Window Opener

We usually use the "open" method in the window object to open a new window in Javascript. However, sometimes you need to change some values in the parent window depending on the changes of the new window elements values or you may need to call a method in the parent window from the child one. To do so, you can use the "opener" property in the window object to access the parent page elements. The following Javascript statement access an input element in the parent window with "txtEmail" ID and update its value:

window.opener.document.getElementById("txtEmail").value = "mohammedn@mailhost.com";
You can even call a method direct in the parent window from the new opened one:
window.opener.SomeMethodInTheParentWindow();
However, you may get "Access Denied" or "Permission Denied" message when trying to access the "opener" property in the popup window. This usually because you call the window.open with the full path of the pop window:
window.open('http://www.domain.com/accounts/register_popup.aspx');
To resolve this issue, use the relative path of the popup page instead of the full path when opening it and you will be able to safely access the opener property of the window.
window.open('accounts/register_popup.aspx');

2 comments:

Anonymous said...

But In case your popup is from another server you have to specify full path. In that case how can we avoid this error?

Mohammed Nour said...

You may open another window in your application which then redirects either from server side or from the javascript to the external domain URL.