4

如果主机名与父级和子级相同,我正在尝试关闭子窗口,但它的

<script type="text/javascript">
    $(document).ready(function () {
        if (window.opener) {
            if (window.opener.location.indexOf(document.location.hostname) != -1) {
                window.opener.location = window.location;
                window.close();
            }
        }
    });
</script>

并得到这个错误

Error: window.opener.location.indexOf is not a function
Source File: https://example.com/default
Line: 100
4

2 回答 2

10

location对象不是字符串、数组或任何其他具有indexOf方法的对象。也许您打算使用opener.location.href.indexOf(...)?

于 2011-12-24T05:54:45.997 回答
2

问题是它location不是一个String,它是一个Location对象。您可以使用toString方法location将其转换为字符串:

window.opener.location.toString().indexOf(document.location.hostname)
于 2011-12-24T05:54:55.977 回答