JavaScript + Chrome Tabs Api - 无法获取标签的URL [英] JavaScript + Chrome Tabs Api - can't get tab's URL
本文介绍了JavaScript + Chrome Tabs Api - 无法获取标签的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下一段代码,问题在于chrome.tabs.getSelected的回调是在用空url发送的请求之后进行评估的。如何解决这个问题?
I have the following piece of code and the problem is that the callback from chrome.tabs.getSelected is evaluated after the request which is send with empty url. How can I solve this?
function send() {
var url = '';
chrome.tabs.getSelected(null, function(tab) {
url = tab.url;
});
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
if(this.readyState == 4) {
alert(this.status);
}
}
client.open("POST", "http://myurl");
client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
client.send(url);
}
推荐答案
欢迎使用Asynchronous Programming p>
Welcome to Asynchronous Programming
function send() {
chrome.tabs.getSelected(null, function(tab) {
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
if(this.readyState == 4) {
alert(this.status);
}
}
client.open("POST", "http://myurl");
client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
client.send(tab.url);
});
}
这篇关于JavaScript + Chrome Tabs Api - 无法获取标签的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文