Question

I am trying to close child window if host name are same of parent and child but its

<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>

and getting this error

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

Solution

The location object is not a string, array, or any other object which has an indexOf method. Perhaps you meant to use opener.location.href.indexOf(...)?

OTHER TIPS

The problem is that location is not a String, it is a Location object. You can use toString method of location to convert it to string:

window.opener.location.toString().indexOf(document.location.hostname)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top