2015-12-08 6 views
-2

Я новичок в JavaScript, и мне трудно понять этот встроенный скрипт. Может ли кто-нибудь объяснить это мне смысл кода и как он работает? Ваше время и помощь будут очень признательны!Что делает этот скрипт?

<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> 
+0

Этот скрипт работает? – azad

+0

да, но у меня возникли проблемы с пониманием того, как это работает. Пожалуйста помоги!! – Jess

+0

(функция (d, s, id) {})(); – yaochiqkl

ответ

1

Как уже было сказано, эта функция загружает виджет 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"); 
0

если Donot существует сценарий с идентификатором твиттер-WJS, а затем вставить скрипт с ЦСИ equels http://platform.twitter.com/widgets.js. И это самопроизвольно.

1

Это то же самое, как:

<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> 

Кроме этого не загрязняет глобальные переменные в документе, потому что это самовыполняющийся функция.

1

Это функция самозапуска, вызываемая тремя документами параметров и строками «script» & «twitter-wjs». Если идентификатор «twitter-wjs» не найден в документе, он создает сценарий сценария с src & id & вставляет скрипт в список тегов скрипта.

Смежные вопросы