Firefox 3.6 |
|
IE 8 |
|
Chrome 10 |
|
Safari 4 |
|
Opera 11 |
可以看出各个主流浏览器的行为一致。虽然jQueryUI在服务器延迟了2秒钟再返回,但是后引入的内联JavaScript还是等待了2秒,等前面引入的JavaScript执行完毕才执行。这也是著名的JavaScript顺序执行的特性。
3. 通过JavaScript添加script标签(test3.htm)
我们首先定义一个addScript函数,用来引入外部或者内联JavaScript。test3.htm的页面源代码如下:
<script> <br>function addScript(url, inline) { <br>var head = document.getElementsByTagName("head")[0]; <br>var script = document.createElement('script'); <br>script.type = 'text/javascript'; <br>if (inline) { <br>script.text = url; <br>} else { <br>script.src = url; <br>} <br>head.appendChild(script); <br>} <br>$(function () { <br>addScript('./service.ashx?file=js/jquery-ui.js&delay=2000'); <br>addScript('alert(typeof(jQuery.ui));', true); <br>}); <br></script>
我们分别在各种浏览器中测试这个例子:
test3.htm
通过JavaScript添加<script>标签 <br>
<table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN-LEFT: 10.5pt; BORDER-LEFT: medium none; WIDTH: 413.1pt; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellspacing="0" cellpadding="0" width="689" border="1">
<tbody>
<tr>
<td style="BORDER-RIGHT: 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: 1pt solid; WIDTH: 67.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: 1pt solid" valign="top" width="112">
<p align="left"><span>Firefox 3.6</span></p>
</td>
<td style="BORDER-RIGHT: 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 345.85pt; PADDING-TOP: 0cm; BORDER-BOTTOM: 1pt solid" valign="top" width="576">
<p align="left"><img style="max-width:90%" alt="" src="http://files.jb51.net/upload/201103/20110303231405311.gif" style="max-width:90%" border="0"></p>
</td>
</tr>
<tr>
<td style="BORDER-RIGHT: 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: 1pt solid; WIDTH: 67.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: 1pt solid" valign="top" width="112">
<p align="left">IE 8</p>
</td>
<td style="BORDER-RIGHT: 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 345.85pt; PADDING-TOP: 0cm; BORDER-BOTTOM: 1pt solid" valign="top" width="576">
<p align="left"><img style="max-width:90%" alt="" src="http://files.jb51.net/upload/201103/20110303231407246.gif" style="max-width:90%" border="0"></p>
</td>
</tr>
<tr>
<td style="BORDER-RIGHT: 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: 1pt solid; WIDTH: 67.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: 1pt solid" valign="top" width="112">
<p align="left"><span>Chrome 10</span></p>
</td>
<td style="BORDER-RIGHT: 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 345.85pt; PADDING-TOP: 0cm; BORDER-BOTTOM: 1pt solid" valign="top" width="576">
<p align="left"><img style="max-width:90%" alt="" src="http://files.jb51.net/upload/201103/20110303231407150.gif" style="max-width:90%" border="0"></p>
</td>
</tr>
<tr>
<td style="BORDER-RIGHT: 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: 1pt solid; WIDTH: 67.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: 1pt solid" valign="top" width="112">
<p align="left"><span>Safari 4</span></p>
</td>
<td style="BORDER-RIGHT: 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 345.85pt; PADDING-TOP: 0cm; BORDER-BOTTOM: 1pt solid" valign="top" width="576">
<p align="left"><img style="max-width:90%" alt="" src="http://files.jb51.net/upload/201103/20110303231407465.jpg" style="max-width:90%" border="0"></p>
</td>
</tr>
<tr>
<td style="BORDER-RIGHT: 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: 1pt solid; WIDTH: 67.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: 1pt solid" valign="top" width="112">
<p align="left"><span>Opera 11</span></p>
</td>
<td style="BORDER-RIGHT: 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 345.85pt; PADDING-TOP: 0cm; BORDER-BOTTOM: 1pt solid" valign="top" width="576">
<p align="left"><img style="max-width:90%" alt="" src="http://files.jb51.net/upload/201103/20110303231406793.gif" style="max-width:90%" border="0"></p>
</td>
</tr>
</tbody>
</table></span></p>
<p align="left"><span style="FONT-FAMILY: 宋体">可见,通过</span>JavaScript<span style="FONT-FAMILY: 宋体">在</span>DOM<span style="FONT-FAMILY: 宋体">加载完毕后再引入外部或者内联</span>JavaScript<span style="FONT-FAMILY: 宋体">时,</span>Firefox<span style="FONT-FAMILY: 宋体">和</span>Opera<span style="FONT-FAMILY: 宋体">的行为一致,能够确保</span>JavaScript<span style="FONT-FAMILY: 宋体">的执行顺序和引入顺序一致。但是</span>IE8, Chrome, Safari <span style="FONT-FAMILY: 宋体">却不能保证这个执行顺序。</span></p>
<p><span style="FONT-FAMILY: 宋体">虽然各种浏览器在确保执行顺序方面不尽相同,不过这时的最大好处是多个</span>JavaScript<span style="FONT-FAMILY: 宋体">文件能够并行下载,这在所有浏览器中行为一致。当然这不是这篇文章的主题,可以</span><a href="http://www.google.com/search?q=javascript%20parallel%20download">Google<span style="FONT-FAMILY: 宋体"><span>更多细节</span></span></a><span style="FONT-FAMILY: 宋体">。</span></p>
<p><span style="FONT-FAMILY: 宋体">如何解决各个浏览器的不一致性,下面提供了两个解决方案:<br><strong>4. 方案一,如何在动态添加script标签时确保执行顺序 <br></strong>有时页面逻辑要求我们必须通过上面的方式动态执行JavaScript,那么如何确保所有浏览器下的执行顺序(目前只有Firefox和Opera确保执行顺序)。 <br>其实解决方案很简单,我们为函数执行添加一个complete的回调函数就行了。下面的test4.htm给出了具体的解决方案: <br><div class="codetitle">
<span><a style="CURSOR: pointer" data="768" class="copybut" id="copybut768" onclick="doCopy('code768')"><u>复制代码</u></a></span> 代码如下:</div>
<div class="codebody" id="code768"> <br><html> <br><head> <br><title></title> <br><script src="./js/jquery-1.4.4.js" type="text/javascript"></script>
<script> <br>function addScript(url, inline, callback) { <br>var head = document.getElementsByTagName("head")[0]; <br>var script = document.createElement('script'); <br>script.type = 'text/javascript'; <br>if (inline) { <br>script.text = url; <br>} else { <br>script.src = url; <br>script.onload = script.onreadystatechange = function () { <br>if (!script.readyState || script.readyState === 'loaded' || script.readyState === 'complete') { <br>if (callback) { <br>callback(); <br>} <br>script.onload = script.onreadystatechange = null; <br>}; <br>}; <br>} <br>head.appendChild(script); <br>if (inline && callback) { <br>callback(); <br>} <br>} <br>$(function () { <br>addScript('./service.ashx?file=js/jquery-ui.js&delay=2000', false, function () { <br>addScript('alert(typeof(jQuery.ui));', true); <br>}); <br>}); <br></script>
Firefox 3.6 |
|
IE 8 |
|
Chrome 10 |
|
Safari 4 |
|
Opera 11 |
Firefox 3.6 |
|
IE 8 |
|
Chrome 10 |
|
Safari 4 |
|
Opera 11 |