修改!!
我设法在我的console.log中获得alexa排名
function() {
var a = document.createElement('a');
a.href = this.url;
var domain = a.hostname,
req = new XMLHttpRequest(),
cors = 'https://cors-anywhere.herokuapp.com/';
function init(){
req.addEventListener('load', function(){
var r, n;
r = /<POPULARITY[^>]*TEXT="(\d+)"/g
n = r.exec(this.response)
console.log("alexa rank "+( null != n ? n[1] : -1 ))
});
req.open('GET', cors+'http://data.alexa.com/data?cli=10&dat=s&url='+domain);
req.send();
};
return init();
Console.log(使用google.com作为测试):
alexa rank 1
如何让console.log结果作为值返回? 我已经尝试了这个
return "alexa rank "+( null != n ? n[1] : -1 );
return ("alexa rank "+( null != n ? n[1] : -1 );
return = "alexa rank "+( null != n ? n[1] : -1 );
有什么建议吗?