就像在LinkedIn的api示例中一样:
<script type="text/javascript" src="https://platform.linkedin.com/in.js">
api_key: weqrw1zwufdsiot9434re
onLoad: onLinkedInLoad
authorize: true
</script>
答案 0 :(得分:9)
在具有script
属性的src
代码中,代码的内容不会被处理为JavaScript。该标记允许拥有内容,但该内容为script documentation,默认情况下浏览器不会处理该内容。 LinkedIn API完全有可能以某种方式使用该文本(因为它可以从元素中检索它),可能是一系列name:value
对,但它不是JavaScript。
答案 1 :(得分:2)
在这种情况下,什么都没有 - script[src]
元素不执行其内容。但是,脚本本身可以将这些内容用作字符串并按照它想要的方式处理它 - 虽然看到JSON以这种方式传递可能更常见,但确实没有限制。
作为如何自行使用此示例的示例,您的外部脚本可能包含:
var scripts = document.getElementsByTagName("script"),
thisScript = scripts[scripts.length-1];
// The above works because, at the time of execution,
// the current script is the last one on the page
// (unless "defer" is used, but just don't use it :p)
var text = thisScript.textContent || thisScript.innerText,
lines = text.split("\n"),
map = {}, kv, l = lines.length, i;
for( i=0; i<l; i++) {
kv = lines[i].split(":");
if( kv.length < 2) continue; // probably a blank line
map[kv.shift().replace(/^\s+|\s+$/g,'')] = kv.join(":").replace(/^\s+|\s+$/g,'');
// the "shift / join" shenanigans allows for colons in values without breaking
}
// you can now use map.api_key etc.