2013-04-07 82 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

相關問題