2012-02-09 20 views
0

以下是我在Chrome擴展中使用的代碼,它從本地存儲獲取URL並將其與網頁的當前網址進行比較..如何將變量與Chrome擴展中的本地存儲進行比較?

問題是,即使它們是一個,相同。(下面以一個HTTP =://www.google.com和b = HTTP://www.google.com)

var a=""; 
chrome.extension.sendRequest({method: "getLocalStorage", key: "url"}, function(response) { 
a=response.data; 
}); 
var b = document.location; 

//I am trying to compare a and b but I always get they are equal even though they are one and the same 

if (a==b) 
{ 
    alert("Equal"); 
} 

回答

2

您需要等待resp onse回調函數。改變你的代碼:

var a=""; 
chrome.extension.sendRequest({method: "getLocalStorage", key: "url"}, function(response) { 
    a=response.data; 
    var b = document.location; 
    if (a==b) 
    { 
    alert("Equal"); 
    } 
}); 
+0

,我會嘗試,但即使我的代碼它顯示正確only..When我使用一個單獨的警告框顯示,它的正確顯示,而這不是正確的比較.. – Shan 2012-02-09 08:58:03

+0

嘗試使用'console.log()'代替'alert()'。您可以在代碼中的每一行後添加'console.log()',並查看真實的代碼工作流程。或者使用腳本debuger一步一步。 – anfilat 2012-02-10 05:41:12

0

什麼是變量a 的數據類型怎麼這樣的

function (response) { 
    a += response.data; 
} 
相關問題