faqts : Computers : Programming : Languages : JavaScript : Windows

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

41 of 50 people (82%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How can I check whether my script can access properties of another window/frame?
How can I check whether my script can access properties of another window/frame?

Apr 12th, 2000 09:13
Martin Honnen,


If a window/frame contains a document from another host than your 
script is loaded from it access to most properties like
  windowRef.location.href
  windowRef.document.links
will result in an error:
  access disallowed ...
or 
  permission denied ...
So to check whether you have access you could overwrite
  window.onerror 
and check a property to trap the error message.
The following contains code which works with NN4 to provide such a 
check. (It would need to be changed for IE to check for the different 
error message.)

<HTML>
<HEAD>
<SCRIPT>
function checkAccess (win, noAccessHandler, accessHandler) {
  window.oldOnError = window.onerror;
  window.onerror = function (err, url, lineNo) {
    if (err.indexOf('access disallowed') != -1) {
       if (noAccessHandler)
         noAccessHandler();
       return true;
    }
    return false;
  }
  var l = win.location.href;
  window.onerror = window.oldOnError;
  if (accessHandler)
    accessHandler();
}
</SCRIPT>
<SCRIPT>
var win = open('http://javascript.faqts.com');
window.focus();
</SCRIPT>
</HEAD>
<BODY>
<A HREF="javascript: 
         checkAccess (win, 
           function() { alert('no access'); },
           null);
         "
>
check win
</A>
|
<A HREF="javascript: win.location.href = location.href;
         setTimeout(
'checkAccess (win, null, function () { alert
(win.document.links.length); });', 1000);
         void 0"
>
set location and check
</A>
</BODY>
</HTML>