鉴于以下示例,为什么outerScopeVar在所有情况下都未定义?
var outerScopeVar;
var img = document.createElement('img');
img.onload = function() {
outerScopeVar = this.width;
};
img.src = 'lolcat.png';
alert(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)
var outerScopeVar;
setTimeout(function() {
outerScopeVar = 'Hello Asynchronous World!';
}, 0);
alert(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)
// Example using some jQuery
var outerScopeVar;
$.post('loldog', function(response) {
outerScopeVar = response;
});
alert(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)
// Node.js example
var outerScopeVar;
fs.readFile('./catdog.html', function(err, data) {
outerScopeVar = data;
});
console.log(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)
// with promises
var outerScopeVar;
myPromise.then(function (response) {
outerScopeVar = response;
});
console.log(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)
// geolocation API
var outerScopeVar; …Run Code Online (Sandbox Code Playgroud) 我有一个用户连接时运行的内容脚本"*://www.youtube.com/*".我需要知道网址何时发生变化,因为当您点击链接时,YouTube没有打开新的"页面",可以这么说.而是它改变了URL和页面内容(我认为).因此,我的内容脚本没有意识到任何事情都发生了变化.(我知道这是因为当我重新加载页面时它会起作用)
那么如何找出我的脚本运行的选项卡的URL何时发生了变化?
[编辑]:我的问题涉及专门获取运行内容脚本的选项卡的URL,而不是获取活动选项卡的URL.