views:

53

answers:

1

What is the best way to determine whether a frame is cross-domain? Here's what I have now:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script>
$(document).ready(function() {
    $("#foo").load(function() {
        alert(isCrossDomain(window.frames["foo"]));
    });
});

function isCrossDomain(frame) {
    try {
        var test = frame.document.location.href;
        return false;
    } catch (e) {
        return true;
    }
}
</script>
</head>
<body>
<iframe src="frame.html" id="foo" name="foo"></iframe>
</body>
</html>

Basically, the browser throws an exception when I try to access the frame's location.href property if it's cross-domain. But is it safe to rely on this behavior for all browsers? Is there a better way to implement the isCrossDomain function?

Thanks.

A: 

Can't you just get the src attribute of the IFRAME and check if protocol, port and host are different?

Mic
The src attribute doesn't change when the iframe location changes. And, no, you can't access that information when it's cross-domain.
Emmett