Javascript 这个脚本做什么?
我是JavaScript新手,理解这个嵌入式脚本有困难。有人能给我解释一下代码的含义和工作原理吗?非常感谢您的时间和帮助Javascript 这个脚本做什么?,javascript,html,css,Javascript,Html,Css,我是JavaScript新手,理解这个嵌入式脚本有困难。有人能给我解释一下代码的含义和工作原理吗?非常感谢您的时间和帮助 <body> <div class="trapdoor"> <div class="top door"> </div> <div class="bottom door"> </div> <a href="http
<body>
<div class="trapdoor">
<div class="top door">
</div>
<div class="bottom door">
</div>
<a href="https://twitter.com/twholman"
class="twitter-follow-button"
data-show-count="false"
data-size="large"
data-dnt="false">
Follow @twholman
</a>
<script>!function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (!d.getElementById(id)) {
js = d.createElement(s);
js.id = id;
js.src = "http://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
}
}(document, "script", "twitter-wjs");
</script>
</div>
</body>
!功能(d、s、id){
var js,fjs=d.getElementsByTagName[0];
如果(!d.getElementById(id)){
js=d.createElement;
js.id=id;
js.src=”http://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js,fjs);
}
}(文件,“脚本”、“推特wjs”);
如果不存在id为twitter wjs的脚本,请插入带有src equals的脚本。它是自动执行的。正如前面所说,这个函数加载一个Twitter小部件。这里有一个简短而肮脏的解释
// shorthand for a self-invoking function that accepts 3 named arguments
!function (d, s, id) {
// defines 2 variables, the second being the first "s" element
// in the document (zero-based index)
var js, fjs = d.getElementsByTagName(s)[0];
// checks whether the element with passed-in ID doesn't exist
if (!d.getElementById(id)) {
// if not, create it
js = d.createElement(s);
// assign the earlier argument as an ID property of the element
js.id = id;
// define the source property of the element
js.src = "http://platform.twitter.com/widgets.js";
// add the element to the document
fjs.parentNode.insertBefore(js, fjs);
}
// name and scope the function
}(document, "script", "twitter-wjs");
这与:
<script>
//If the twitter SCRIPT element doesn't exist in the document yet...
if(!document.getElementById('twitter-wjs'))
{
//Make a new script element
var s=document.createElement('script');
//set its id so we know it exists after we insert into the document
s.id='twitter-wjs';
//the external script we want to run
s.src='http://platform.twitter.com/widgets.js';
//The first script block in the document, which could be this one
var firstScript=document.getElementsByTagName('script')[0];
//Now we insert our new external script before the first script in the document
firstScript.parentNode.insertBefore(s, firstScript);
}
</script>
//如果文档中还不存在twitter脚本元素。。。
如果(!document.getElementById('twitter-wjs'))
{
//创建一个新的脚本元素
var s=document.createElement('script');
//设置它的id,以便我们在插入文档后知道它存在
s、 id='twitter-wjs';
//我们要运行的外部脚本
s、 src='1〕http://platform.twitter.com/widgets.js';
//文档中的第一个脚本块,可能是这个
var firstScript=document.getElementsByTagName('script')[0];
//现在,我们在文档中的第一个脚本之前插入新的外部脚本
firstScript.parentNode.insertBefore(s,firstScript);
}
但它不会污染文档中的全局变量,因为它是一个自运行函数。它是一个自调用函数,使用三个参数文档和字符串“script”和“twitter wjs”调用。若在文档中找不到“twitterwjs”id,则创建
带有src&id的脚本标记&在脚本标记列表中插入脚本。此脚本有效吗?是的,但我很难理解它是如何工作的。请帮忙!!(函数(d,s,id){}();加载twitters
widgets.js