像LinkedIn的API例如:什麼隨機冒號意味着在JavaScript
<script type="text/javascript" src="https://platform.linkedin.com/in.js">
api_key: weqrw1zwufdsiot9434re
onLoad: onLinkedInLoad
authorize: true
</script>
像LinkedIn的API例如:什麼隨機冒號意味着在JavaScript
<script type="text/javascript" src="https://platform.linkedin.com/in.js">
api_key: weqrw1zwufdsiot9434re
onLoad: onLinkedInLoad
authorize: true
</script>
在script
標籤與src
屬性,標籤的內容不被處理,如JavaScript。該標籤允許擁有內容,但該內容爲script documentation,默認情況下瀏覽器不處理該內容。 LinkedIn API完全有可能以某種方式使用該文本(因爲它可以從元素中檢索它),可能是一系列name:value
對,但它不是JavaScript。
有趣的是[John Resig考慮改變這個](http://ejohn.org/blog/degrading-script-tags/)(處理一個'
在這種情況下,什麼也沒有 - 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.
什麼用downvotes的?完全合理的問題。 – 2014-10-07 17:58:02
標題不明確......「隨機冒號」...? – webeno 2014-10-07 18:01:25