2012-02-09 23 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; 
} 
相关问题