2011-12-24 46 views
4

我试图关闭子窗口,如果主机名是相同的父母和孩子,但其window.opener.location.indexOf()函数问题

<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 

回答

9

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

+0

哦~~!感谢这一点。 :) +1。 – 2011-12-24 06:00:54

2

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

window.opener.location.toString().indexOf(document.location.hostname) 
+1

更好地抓住一个特定的属性(比如* href *)而不是调用* toString()*,因为它更明显使用哪个值,属性访问比调用方法更有效。 – RobG 2011-12-24 06:14:43