Evangelism/Firefox3.5/35Days/Articles/Defer on script elements: Difference between revisions

m
Line 11: Line 11:
Here is a simple first test to see how the attribute works. The following lines are in the <tt>head</tt> element of a page :
Here is a simple first test to see how the attribute works. The following lines are in the <tt>head</tt> element of a page :


  &#139;script&#155;
  &#60;script&#62;
  var test1 = &#34;Test 1 : fail&#34;;
  var test1 = &#34;Test 1 : fail&#34;;
  &#139;/script&#155;
  &#60;/script&#62;
  &#139;script <font style="color: green;">defer</font>&#155;
  &#60;script <font style="color: green;">defer</font>&#62;
   console.log(test1);
   console.log(test1);
  &#139;/script&#155;
  &#60;/script&#62;
  &#139;script&#155;
  &#60;script&#62;
  test1 = &#34;Test 1 : pass&#34;;
  test1 = &#34;Test 1 : pass&#34;;
  &#139;/script&#155;
  &#60;/script&#62;


If the <tt>defer</tt> attribute for the <tt>script</tt> element is correctly implemented in the browser which renders the page, the second script element is executed after all the others, and the [https://addons.mozilla.org/en-US/firefox/addon/1843 Firebug] console should display <tt><font style="color: blue;">"Test 1 : pass"</font></tt>.<br> Otherwise, the console displays  <tt><font style="color: blue;">"Test 1 : fail"</font></tt> because the scripts are executed in the same order than in the source code.
If the <tt>defer</tt> attribute for the <tt>script</tt> element is correctly implemented in the browser which renders the page, the second script element is executed after all the others, and the [https://addons.mozilla.org/en-US/firefox/addon/1843 Firebug] console should display <tt><font style="color: blue;">"Test 1 : pass"</font></tt>.<br> Otherwise, the console displays  <tt><font style="color: blue;">"Test 1 : fail"</font></tt> because the scripts are executed in the same order than in the source code.