Google API的客户端库不执行回调
Google API's client library does not execute callback
所有 Google API 描述都提到在 Javascript 客户端库加载后,这应该触发它提供给 ?onload
的功能
这是我写的测试代码,但是myFunc没有被激活。有人可以帮忙吗?
<!DOCTYPE html>
<html>
<head>
<title> Test </title>
<meta charset='utf-8' />
<script>
function myFunc(){
document.write( "Hi" );
}
</script>
<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=myFunc"></script>
</head>
<body>
</body>
</html>
您的引号有点乱。尝试将引号放在脚本位置的末尾,从 src
属性 中删除 onload
。然后用括号将 onload
属性 设置为 myFunc()
。
<!DOCTYPE html>
<html>
<head>
<title> Test </title>
<meta charset='utf-8' />
<script>
function myFunc(){
document.write( "Hi" );
}
</script>
<script type="text/javascript"
src="//apis.google.com/js/client.js"
onload="myFunc()"></script>
</head>
<body>
</body>
</html>
Firefox 在控制台中说:
A call to document.write() from an asynchronously-loaded external script was ignored.
Chrome 说了类似的话:
Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
尝试使用 document.write
以外的东西。
<script>
function myFunc(){
document.body.innerHTML='oh yeah';
}
</script>
<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=myFunc"></script>
- 不鼓励使用
document.write
,因为如果在加载文档后执行它会替换整个文档。
- Learn how to debug JavaScript.
函数执行完毕
问题似乎在 document.write();
它在 chrome 控制台
上生成以下错误
无法在 'Document' 上执行 'write':除非显式打开,否则无法从异步加载的外部脚本写入文档。
所有 Google API 描述都提到在 Javascript 客户端库加载后,这应该触发它提供给 ?onload
这是我写的测试代码,但是myFunc没有被激活。有人可以帮忙吗?
<!DOCTYPE html>
<html>
<head>
<title> Test </title>
<meta charset='utf-8' />
<script>
function myFunc(){
document.write( "Hi" );
}
</script>
<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=myFunc"></script>
</head>
<body>
</body>
</html>
您的引号有点乱。尝试将引号放在脚本位置的末尾,从 src
属性 中删除 onload
。然后用括号将 onload
属性 设置为 myFunc()
。
<!DOCTYPE html>
<html>
<head>
<title> Test </title>
<meta charset='utf-8' />
<script>
function myFunc(){
document.write( "Hi" );
}
</script>
<script type="text/javascript"
src="//apis.google.com/js/client.js"
onload="myFunc()"></script>
</head>
<body>
</body>
</html>
Firefox 在控制台中说:
A call to document.write() from an asynchronously-loaded external script was ignored.
Chrome 说了类似的话:
Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
尝试使用 document.write
以外的东西。
<script>
function myFunc(){
document.body.innerHTML='oh yeah';
}
</script>
<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=myFunc"></script>
- 不鼓励使用
document.write
,因为如果在加载文档后执行它会替换整个文档。 - Learn how to debug JavaScript.
函数执行完毕 问题似乎在 document.write(); 它在 chrome 控制台
上生成以下错误无法在 'Document' 上执行 'write':除非显式打开,否则无法从异步加载的外部脚本写入文档。