Best way to determine if a frame is cross-domain?

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

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<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 location location.href property if it is a cross-domain. But can you rely on this behavior for all browsers? Is there a better way to implement the isCrossDomain function?

Thank.

+3
source share
1 answer

Can you get the srcIFRAME attribute and check if the protocol, port and host are different?

0
source

Source: https://habr.com/ru/post/1738501/


All Articles