2013-04-07 83 views
0

我需要检查该位置是否与某些文本相似。所以,我的代码如下:为什么我不能在window.location上搜索?

 //are we already on the same form 
     var loc = window.top.window.location; 
     if(loc) 
      window.alert("loc=" + loc); 

     if(loc && loc.search(form, "i") != -1) 
     { 

     } 

的window.top.window看上去有些奇怪 - 它的使用,因为窗口可能不是最上面,我需要得到最上面的实例。

我确实得到了一个loc实例 - 所以它不是null。但搜索工作?

但是,如果我运行此代码,我得到一个JavaScript运行时错误:

Caught exception: Object doesn't support this action 

为什么我得到这个问题?

如果我无法搜索如何比较使用位置的字符串?

编辑

什么让我感到困惑的是,位置确实有一个只读属性搜索的是HTTP GET命令。

我在想我在做一个字符串搜索 - 而是试图写入一个只读属性。

回答

2

尝试:

 window.top.window.location.href 

你可以提醒只字符串。 window.location变量是一个对象,什么是href属性,什么是字符串。 请参阅该文档:http://www.w3schools.com/jsref/obj_location.asp

如果你想看到什么位置对象内:

 console.log(window.top.window.location); 

这将打印这样的事情(在Chrome):

Location 
     -ancestorOrigins: DOMStringList 
     -assign: function() { [native code] } 
     -hash: "" 
     -host: "stackoverflow.com" 
     -hostname: "stackoverflow.com" 
     -href: "http://stackoverflow.com/posts/15863038/edit" 
     -origin: "http://stackoverflow.com" 
     -pathname: "/posts/15863038/edit" 
     -port: "" 
     -protocol: "http:" 
     -reload: function() { [native code] } 
     -replace: function() { [native code] } 
     -search: "" 
     -toString: function toString() { [native code] } 
     -valueOf: function valueOf() { [native code] } 
     -__proto__: Location 
      ... 
1

您需要使用window.top.window.location.hrefwindow.top.window.location是一个对象而不是一个字符串。

//are we already on the same form 
    var loc = window.top.window.location.href; 
    if(loc) 
     window.alert("loc=" + loc); 

    if(loc && loc.search(form, "i") != -1) 
    { 

    } 

image

相关问题