Function window.opener.location.indexOf ()

I try to close the child window if the host name matches the parent and child, but

<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 get this error

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

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

+9
source

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

 window.opener.location.toString().indexOf(document.location.hostname) 
+2
source

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


All Articles