2013-03-16 56 views
0

我得到了一個類似按鈕插件代碼的facebook自動生成的函數。不過,我對函數中的變量不太確定,如果有人幫我弄清楚每個元素的含義,我會非常感激。確定函數的元素javascript

html功能從收集數據:

<div class="fb-like" data-href="https://www.facebook.com/yanntiersen.official" 
data-send="true" data-width="450" data-show-faces="true" data-font="arial"></div> 

實際的JavaScript函數:

<script>(function(d, s, id) { 
    var js, fjs = d.getElementsByTagName(s)[0]; 
    if (d.getElementById(id)) return; 
    js = d.createElement(s); js.id = id; 
    js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=123456789"; 
    fjs.parentNode.insertBefore(js, fjs); 
}(document, 'script', 'facebook-jssdk'));</script> 

我看到函數被調用爲(document, 'script', 'facebook-jssdk'),但我真的不明白怎麼會事工作。 有誰能解釋我這個嗎?

+0

Bujanca,你問了一個問題,並得到了一個答案的負載。你真正想知道的是與你實際詢問的完全不同的問題。我的建議是爲第二部分創建一個新問題,儘管你可能得不到很多回應,因爲它很一般。 – 2013-03-16 17:14:17

+0

好的,謝謝.. – 2013-03-16 17:15:15

回答

3
(function (d , s , id) { 

}(document, 'script', 'facebook-jssdk)); 

將意味着以下各項爲真

d = document; 
s = 'script'; 
id = 'facebook-jssdk'; 

這方面的知識

​​

希望幫助

+0

所以有什麼辦法可以找到我的html和js函數之間的關聯嗎?我想在不同參數的循環中執行這個函數。而不是'https:// www.facebook.com/yanntiersen.official'我會有一個鏈接陣列 – 2013-03-16 17:02:51

+0

你爲什麼要這樣做?這個函數只是將facebook的JDK異步加載到DOM中。你可以一次只調用一次... – 2013-03-16 17:04:32

+0

我將編輯問題描述 – 2013-03-16 17:07:28

1

腳本正在尋找標記名爲'script'的元素,然後獲取對第一個腳本對象的引用。

然後查找ID爲「facebook-jssdk」的元素。如果它發現這個ID,它會停止處理。

然後創建一個新的腳本,設置ID爲「Facebook的jssdk」,源頭上「//connect.facebook.net/en_GB/all.js#xfbml=1 &的appid = 123456789」,並在頁面上的第一個腳本之前插入腳本。

+0

那麼有沒有什麼辦法可以找到我的html和js函數之間的關聯?我想在不同參數的循環中執行這個函數。而不是'https:// www.facebook.com/yanntiersen.official'我會有一個鏈接陣列 – 2013-03-16 17:02:25

+0

所有這些都是添加一個JavaScript文件。沒有必要多次運行它。 – 2013-03-16 17:05:49

2
//Create a function taking arguments `d`, `s` and `id` 
(function(d, s, id) { 

    //Initialise variable js, initialise variable fjs to the first element with the tag 
    //name s 
    var js, fjs = d.getElementsByTagName(s)[0]; 

    //If the element with id = id exists already, escape from the function. 
    if (d.getElementById(id)) return; 

    //Create a new element of type s ('script') and set it's id to id ('facebookjssdk') 
    js = d.createElement(s); js.id = id; 

    //Set the script's src attribute to the path specified. 
    js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=123456789"; 

    //Insert the new element immediately before the first <script> on the page. 
    fjs.parentNode.insertBefore(js, fjs); 

//Call the function immediately with document for d, 'script' for s and 'facebook-jssdk' 
//for id. 
}(document, 'script', 'facebook-jssdk')); 

如果你不熟悉的模式(function() {})();,它只是一個封裝它的代碼和它的定義調用自身的功能。

+0

有沒有什麼辦法可以找到我的html和js函數之間的關聯?我想在不同參數的循環中執行這個函數。而不是'https:// www.facebook.com/yanntiersen.official'我會有一個鏈接數組 – 2013-03-16 17:03:22

+0

我不確定你的意思,這個腳本只能生成一個元素(#facebook-jssdk)和它創建了一個單一的事物的鏈接。你指的是什麼鏈接? – 2013-03-16 17:05:38

+0

我將編輯問題描述 – 2013-03-16 17:07:09

2

基本上它這樣做:

Gets the first "<script>" element in the document 
If an object exists with the id of "facebook-jssdk" than 
    return (dont process the rest of the code) 
EndIf 
Create a "<script>" element 
Set the new "<script>" element's id to 'facebook-jssdk' 
Set the new "<script>" element's src location to "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=123456789" 
(this loads a javascript file from facebook's server onto the client) 
Insert the new "<script>" tag before the first "<script>" tag in the page 

希望幫助:)